これはなぜ動作しないのか分かりません。場合によってはword_1
がword
に、時には空白で_
に置き換えられます。Python。さまざまなステップで.replaceと単語を置き換えてください
アイデア?これは私にとっては非常に奇妙に見えます。
f1 = open(file_name, 'r')
f2 = open(file_name + '.tmp', 'w')
for line in f1:
f2.write(line.replace('word_1', 'word'))
f1.close()
f2.close()`
f1 = open(file_name, 'r')
f2 = open(file_name + '.tmp', 'w')
for line in f1:
f2.write(line.replace('word_2', 'word'))
f1.close()
f2.close()
f1 = open(file_name, 'r')
f2 = open(file_name + '.tmp', 'w')
for line in f1:
f2.write(line.replace('_', ' '))
f1.close()
f2.close()
元のファイルから読み込み、一時ファイルを上書きすることに注意してください。最後の置換のみが出力ファイルに影響します。 – dhke