Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSVWriter not saving data to file the moment I write it

Tags:

python

csv

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 ....

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

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.

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:

  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?
like image 769
skyeagle Avatar asked Oct 20 '10 10:10

skyeagle


People also ask

How do I write to CSV?

Steps for writing a CSV file First, open the CSV file for writing ( w mode) by using the open() function. Second, create a CSV writer object by calling the writer() function of the csv module. Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.

Do I need to close CSV writer?

Close a CSV File You don't want an open file running around taking up resources! Use the close() function to close an open file.

Why is my csv file empty Python?

The reason that you see nothing in the file is that python still has the file open. You need to close it. Save this answer.

How do I write a csv file in Python?

Write CSV files with csv.DictWriter() class can be used to write to a CSV file from a Python dictionary. Here, file - CSV file where we want to write to. fieldnames - a list object which should contain the column headers specifying the order in which data should be written in the CSV file.


2 Answers

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.

like image 198
Tim Pietzcker Avatar answered Sep 20 '22 18:09

Tim Pietzcker


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

like image 35
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 18:09

Ignacio Vazquez-Abrams