我使用以下代码在一行中插入文本:
exp = 82
with open ('test.txt','r') as b:
lines = b.readlines()
with open('test.txt','w') as b:
for i,line in enumarate(lines):
if i == exp:
f.write('test_data')
f.write(line)
这将在第82行插入文本。如何修改它,使它可以在第82行插入文本,当它第一次运行时,在第83行,然后在第84行等等。我想使用一个计数器,但我不确定。在
Tags:
如果不想完全循环该文件,可以使用以下命令:
对于原始测试输入:
^{pr2}$我得到了以下信息:
如果您有一个巨大的文件,您无法将其加载到RAM中(请参见阅读)您可能希望使用其他一些解决方案进行枚举和逐行处理。 希望这有帮助!在
方法是增加一个计数器。在
您可以使用文本文件来存储代码已经运行了多少次。从文本文件中读取此值并将其添加到ex中。然后增加代码运行的次数,将其写入文本文件,然后运行您编写的代码。在
相关问题 更多 >
编程相关推荐