我有一个文件。我想用哈希 (#) 评论那些有文本的行是“测试”。
一个.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()
请帮忙