私はファイルを持っています。テキストが "test" である行にハッシュ (#) を付けてコメントしたいと思います。
a.txt:
this is test
test is this
this test is good
this good
したがって、出力は...
#this is test
#test is this
#this test is good
this good
正規表現を試しましたが、完全には機能しませんでした。
def convert_string():
import re
fh=open("tt.txt","rb")
lines=fh.readlines()
for line in lines:
line= re.sub(r'^(test)',r'#\1',line)
print line
fh.close()
助けてください