这个例程对我来说看起来不错,但最终将垃圾写入文件。lines_of_interest
是一组需要在文件中更改的行( 896227L
、425200L
、等)。640221L
if else 例程确定该行上的更改内容。这是我第一次使用seek()
但相信语法是正确的。任何人都可以在代码中发现任何可以使其正常工作的错误吗?
outfile = open(OversightFile, 'r+')
for lines in lines_of_interest:
for change_this in outfile:
line = change_this.decode('utf8', 'replace')
outfile.seek(lines)
if replacevalue in line:
line = line.replace(replacevalue, addValue)
outfile.write(line.encode('utf8', 'replace'))
break#Only check 1 line
elif not addValue in line:
#line.extend(('_w\t1\t'))
line = line.replace("\t\n", addValue+"\n")
outfile.write(line.encode('utf8', 'replace'))
break#Only check 1 line
outfile.close()