次のような複数行のファイルがあります。
'AMS_Investigation|txtt.co_BigtittedBlondOtherNight_1371078139195_+14155186442', {'cf:rv': '0'}
1371078139195 (この場合) を別の番号に置き換えたい。置き換えたい値は常に最初のカンマ区切りの単語にあり、常にその単語の最後から 2 番目のアンダースコア区切りの値です。以下は私がこれを行った方法であり、機能しますが、これは見苦しく不器用に思えます。
>>> line="'AMS_Investigation|txtt.co_BigtittedBlondOtherNight_1371078139195_+14155186442', {'cf:rv': '0'}"
>>> l1=",".join(line.split(",")[1:])
>>> print l1
{'cf:rv': '0'}
>>> l2=line.split(",")[0]
>>> print l2
'AMS_Investigation|txtt.co_BigtittedBlondOtherNight_1371078139195_+14155186442'
>>> print "_".join(l2.split('_')[:-2])
'AMS_Investigation|txtt.co_BigtittedBlondOtherNight
>>>
>>> print "_".join(l2.split('_')[:-2])+ "_1234567_"+(l2.split('_')[-1])
'AMS_Investigation|txtt.co_BigtittedBlondOtherNight_1234567_+14155186442'
>>> print "_".join(l2.split('_')[:-2])+ "_1234567_"+(l2.split('_')[-1]) + "," + l1
'AMS_Investigation|txtt.co_BigtittedBlondOtherNight_1234567_+14155186442', {'cf:rv': '0'}
>>>
値を (おそらく正規表現を使用して) 置き換える簡単な方法はありますか? これが最善の方法だとは思えない
私にはいくつかの答えがありますが、それが最後から 2 番目に強調された値であることを強調しなければなりません。以下は有効な文字列です。
line = "'AMS_Investigation|txtt.co_23456_BigtittedBlondOtherNight_1371078139195_+14155186442', {'cf:rv': '0'}"
line = "'AMS_Investigation|txtt.co_23456_BigtittedBlondOtherNight_1371078139195_14155186442', {'cf:rv': '0'}"
line = "'AMS_Investigation|txtt.co_1371078139195_BigtittedBlondOtherNight_1371078139195_1371078139195', {'cf:rv': '0'}"
上記の場合、文字列内に最後の 2 番目のアンダースコアの後にない数字文字列があります。また、最後の部分はすべて数字である場合とそうでない場合があります (+14155186442 または 14155186442 の可能性があります)。申し訳ありませんが、これについては上記に言及していません。
あ