我有一个脚本,可以将近80万行写到各种CSV文件中。
for col in data:
with open('output/{}.csv'.format(col), mode, encoding='utf-8', newline='') as f:
writer = csv.writer(f, lineterminator = '\n')
if mode == 'w':
writer.writerow(headers)
for row in data[col]:
writer.writerow(row)
在现代机器上,这似乎是正确的工作和写所有的行。
然而,在旧机器上(使用机械硬盘),总共有35%到40%的行丢失。
这里是机器的列表,以及写入的总行(在815143行中):
是我做错了什么事情导致了这一切吗?
还是写作失败需要考虑?使用csv.write
时
发布于 2021-03-12 17:09:05
造成此问题的原因似乎是Python的输出缓冲区。
https://blog.finxter.com/what-is-python-output-buffering-and-how-to-disable-it/
运行python时传递-u
标志似乎解决了csv行未写入较慢计算机的问题。
例:
python -u file.py argument
希望这能帮助其他遇到同样问题的人。
https://stackoverflow.com/questions/66547685
复制相似问题