CSVWriter not saving data to file the moment I write it

前端 未结 3 1528
野趣味
野趣味 2020-11-29 05:29

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 w

相关标签:
3条回答
  • 2020-11-29 06:27

    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.

    0 讨论(0)
  • 2020-11-29 06:27

    Wow, I was in the wrong place, skip this answer!

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

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

    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

    0 讨论(0)
  • 2020-11-29 06:34

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

    0 讨论(0)
提交回复
热议问题