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()

Saturday, 17 November 2012

Blog/YouTube Channel

I don't post to my blog very often yet I have started making a few more videos on my YouTube channel in one of three categories I guess:

- "Tech" (for want of a better name) how to's (or more exactly how I've figure out how to do stuff which might not be the best way...)
- "Education" (for want of a better name) stuff
- Research presentations

I've decided that I'll also throw those videos on here when I put them together (not too sure why, perhaps the best reason is "why not?"). To get started here are 3 past videos:

- Using GitHub to host a website:



- Simulating a Queue: Basic Discrete Event Simulation



- A game theoretical approach to the EMV ED interface