Python CSVWriter 在我写数据的那一刻没有将数据保存到文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3976711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:38:51  来源:igfitidea点击:

CSVWriter not saving data to file the moment I write it

pythoncsv

提问by skyeagle

Python newbie getting a bit frustrated with the csv module. At this rate, it would have been easier if I wrote the file parser myself, but I want to do things the Pythonic way ....

Python 新手对 csv 模块有点沮丧。按照这个速度,如果我自己编写文件解析器会更容易,但我想以 Pythonic 的方式做事......

I have written a little python script that should save my data into a CSV file.

我已经编写了一个小 python 脚本,可以将我的数据保存到 CSV 文件中。

Here is a snippet of my code:

这是我的代码片段:

import csv

wrtr = csv.writer(open('myfile.csv','wb'),delimiter=',', quotechar='"')

for row in rows:
    wrtr.writerow([row.field1,row.field2,row.field3])

The file myfile.csv is created successfully, yet it is empty - but has a lock on it, as its still being used by the Python process. It seems that the data has been written to the file in memory, but it has not yet been flushed to disk.

文件 myfile.csv 已成功创建,但它是空的 - 但有一个锁定,因为它仍在被 Python 进程使用。看起来数据已经写入内存中的文件,但尚未刷新到磁盘。

Since the Python process is holding a lock on the file, then I assume that I am responsible for releasing the lock. Here are my questions:

由于 Python 进程持有文件锁,那么我假设我负责释放锁。以下是我的问题:

  1. How do I get python to flush to disk
  2. How do I close the file that was opened in the csv.writer() method?
  1. 我如何让python刷新到磁盘
  2. 如何关闭在 csv.writer() 方法中打开的文件?

采纳答案by Tim Pietzcker

Use

with open('myfile.csv','wb') as myfile:
    wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
    for row in rows:
        wrtr.writerow([row.field1,row.field2,row.field3])
        myfile.flush() # whenever you want

or

或者

myfile = open('myfile.csv','wb')
wrtr = csv.writer(myfile, delimiter=',', quotechar='"')
for row in rows:
    wrtr.writerow([row.field1,row.field2,row.field3])
    myfile.flush() # whenever you want, and/or
myfile.close() # when you're done.

The nice thing about the first approach is that your file will also be automatically properly closed in case of an Exception.

第一种方法的好处是,如果发生异常,您的文件也将自动正确关闭。

If you want your file object to be anonymous, then it will only be closed when the program exits. When or whether it is flushed depends on the OS - so it might be never until exit.

如果您希望您的文件对象是匿名的,那么它只会在程序退出时关闭。何时或是否刷新取决于操作系统 - 所以它可能永远不会直到退出。

回答by Ignacio Vazquez-Abrams

The flush()and close()methods of the file object. Or use with.

文件对象flush()close()方法。或使用.with

回答by KoalaBear

I've had the same problem. Writing 1000 records in 1 WriteRecordscall, but it would leave the last record(s?) written incomplete.

我遇到了同样的问题。在 1 次WriteRecords调用中写入 1000 条记录,但会使最后一条记录(s?)不完整。

Even calling like fileStream.Flush();does not work.

即使调用 likefileStream.Flush();也不起作用。

When you do the following, it will flush it correctly:

当您执行以下操作时,它将正确刷新它:

csvWriter.Flush();
csvWriter.Context.Writer.Flush();

Seen on: https://github.com/JoshClose/CsvHelper/issues/967#issuecomment-482503533

见:https: //github.com/JoshClose/CsvHelper/issues/967#issuecomment-482503533