【发布时间】:2014-12-05 08:34:19
【问题描述】:
我试图在 Python 中多次读取某些文件的行。
我正在使用这种基本方式:
with open(name, 'r+') as file:
for line in file:
# Do Something with line
这很好,但是如果我想在我仍然打开文件的情况下对每一行进行第二次迭代:
with open(name, 'r+') as file:
for line in file:
# Do Something with line
for line in file:
# Do Something with line, second time
然后它不起作用,我需要打开,然后关闭,然后再次打开我的文件以使其工作。
with open(name, 'r+') as file:
for line in file:
# Do Something with line
with open(name, 'r+') as file:
for line in file:
# Do Something with line
感谢您的回答!
【问题讨论】: