Python CSV — Reading and Writing Simultaneously

Django, Python

Pythons CSV module turns your csv into an iterator for efficiency and your data is gone after you do 

I just wanted a quick way to add and edit entries in our database of wines via a spreadsheet. I suppose I could have used pyExcelerator, but I magically decided to go with CSV. 

Trying to write on the same file as you are reading from, even if you do open(‘blah’, ‘rw+’) won’t exactly work. 

My solution was just to write a function that takes a path as it’s argument, and have the function read from the CSV from that path & create a reader/writer instance, with the writer file being temporary. It’s important to keep a reference to it, like:

fread=open(path, 'r')
fwrite=open(path.replace('.csv','_temp'), 'w')

Then I read as I wrote into this temporary file called whateverfile_temp. Specifically, I needed the csv to be updated with primary key values from the database if an object was created so that future uploads of the same file would not result in duplicates but UPDATE.

I kept having trouble because I couldn’t close the file when I started using it as the official docs do: reader/writer = csv.reader/writer(open('blah','rw')) It just resulted in a mangled csv.

But now I could close the files, then replace the original csv that was just read with the new one written.

os.rename(mypath.replace('.csv', '_temp'), mypath)

Sure, inefficient, but for me it will come in very useful.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s