特定のテキストを含む行をファイルから検索し、その行全体を新しい行に置き換えようとしています。文字列を含む行をPythonに置き換えてください
は、私が使用しようとしている:
pattern = "Hello"
file = open('C:/rtemp/output.txt','w')
for line in file:
if pattern in line:
line = "Hi\n"
file.write(line)
私はというエラーを取得:
io.UnsupportedOperation: not readable
私は私が間違ってやっているかわからないんだけどを、誰かが助けることができますしてください。
http://stackoverflow.com/questions/39086/search-and-replace-aファイル内のファイル中の行またはファイルhttp://stackoverflow.com/questions/17140886/how-to-search-and-replace-text-in-a-file-using-python – MooingRawr
あなたは書き込み専用にファイルを開きます。両方を行うには、これを行う必要があります:http://stackoverflow.com/questions/6648493/open-file-for-both-reading-and-writing – Andy