我是python的新手。我目前正在尝试修改文件中的一行,这是在使用 linecache 捕获此行之后。
示例:
run_D = open('toto.dat','r')
lines = run_D.readlines()
print "Name of the file: ", run_D.name
seq=["%s'retrieve'"]
line_1 = linecache.getline('toto.dat', 51)
lines_runD = run_D.readlines()
run_D.close()
然后,我想修改该行的内容:
lines_runD[50]="%s !yop \n".format(seq) #--> this part seems not working
fic = open('toto.dat','w')
fic.writelines(lines_runD)
fic.close()
我有这个错误:
IndexError:列表索引超出范围
我尝试了许多格式类型,但不幸的是它仍然无法正常工作。你有什么建议吗:)
谢谢你。