Tuesday, 27 November 2012

Importing and Exporting data from and to csv files in python

When I first started using Sage one of the challenges was figuring out how to handle data outside of Sage. I made the terrible mistake of trying to learn Sage without knowing any Python. I've subsequently learnt Python (a language I absolutely love) and thought I'd do a short screencast showing how to manipulate csv files with Python.

Here's the screencast:


I thought I'd put the code I wrote up here as well:

import csv

out=open("data.csv","rb")
data=csv.reader(out)
data=[[row[0],eval(row[1]),eval(row[2])] for row in data]
out.close()

new_data=[[row[0],row[1]+row[2]] for row in data]

out=open("new_data.csv","wb")
output=csv.writer(out)

for row in new_data:
    output.writerow(row)

out.close()

Here's a quick explanation (basically repeating what is in the screencast)

The first line imports the csv module:

import csv

The next few lines open a file for reading (denoted by "rb") and use the csv reader method to import the data (the "eval" function is used to handle the fact that all the data is imported as a string).

out=open("data.csv","rb")
data=csv.reader(out)
data=[[row[0],eval(row[1]),eval(row[2])] for row in data]
out.close()

The next line simply creates a new dataset:

new_data=[[row[0],row[1]+row[2]] for row in data]

We then open/create a file called "new_data" for writing (denoted by "wb") and use the csv writer method to export each row of data:

out=open("new_data.csv","wb")
output=csv.writer(out)

for row in new_data:
    output.writerow(row)

out.close()

1 comment:

  1. Really inspirational to hear someone pursuing their dreams and becoming successful instead of following the traditional path. I have read your article about This topic. I think it's good and impressed to know your service. Thanks for share this Information.
    Import export training

    ReplyDelete