import csv
import os
processed_lines = set()
if os.path.exists('processed_lines.txt'):
with open('processed_lines.txt') as lines_fp:
processed_lines.update(int(line_no) for line_no in lines_fp)
with open('processed_lines.txt', 'a') as lines_fp, \
open('holiday.txt', 'rb') as fp:
for line_no, (category, url) in enumerate(csv.reader(fp), 1):
if line_no not in processed_lines:
# process the line
# ...
lines_fp.write(str(line_no)+'\n') # add it to processed lines file
据我所知,没有什么神奇的方法可以做到这一点,但您可以跟踪哪些行已经在单独的文件中处理过,然后在以后的运行中使用它(如果存在的话)跳过它们。我的意思是:
在你完全处理完文件后,把它擦干净就行了
在读取的每一行上重写文件要容易得多
相关问题 更多 >
编程相关推荐