import csv # using Python module f = open('example_1.csv','rb') # use binary mode if on MS windows d = [i for i in csv.reader(f) ] # use list comprehension to read from file f.close() # close file print d d[0].append('Last Name') d[0].append('First Name') print d[0] for row in d[1:]: # start at 1st, not 0th column. Each row is a list first,last= row[0].split() # split on white-space row.append(last) # append to each row row.append(first) print d #%qtconsole f = open('example_1_out.csv','wb') # write mode binary fw = csv.writer(f) # create csv writer fw.writerows(d) f.close() # close file import numpy as np d = np.loadtxt('example_1.csv',delimiter=',',dtype=str) print d print d.dtype dt = [('name', 'S64'), # The first element of the tuple is our name for each respective column ('dob', 'S64'), # and the second element is the numpy dtype we want for that column. ('years', 'int'), # Here we want years as an integer, not a string ('degree', 'S64'), ] d = np.loadtxt('example_1.csv',delimiter=',',dtype=dt,skiprows=1) # skip the header row print d print d['years'].mean() # using numpy arrays import string n=map(string.split,d['name']) w=array([tuple(i)+tuple(j) for i,j in zip(d,n)], # list comprehension glues tuple-ized rows together dtype=dt+[('first','S64'),('last','S64')]) # append new dtypes to existing list of dtypes # the comments are set to '' to avoid hash marks on the first line. np.savetxt('np_output.csv',w,delimiter=',',fmt='%s',header='name,dob,years,degree,first,last',comments='') import pandas as pd d = pd.read_csv('example_1.csv') print d print type(d) print d.columns print d.DOB # this works great when the column header name has no spaces in it. print d['Name'] # you can also refer to columns using this syntax d['name']=d['Name '] # easily create extra column print d.name # now you can access this column using this syntax d = pd.read_csv('example_1.csv',dtype={'Name ':'S64','DOB':'S64','Years':int,'Degree':'S64'}) print d print d.Years.mean() d = pd.read_csv('example_1.csv',dtype={'Name':'S64', 'DOB':'S64', 'Years':int, 'Degree':'S64'},parse_dates=[1]) # difference in birthdays between Alice Jones and John Book print d.DOB[0] - d.DOB[2] %qtconsole d['first']=map(lambda x:string.split(x)[0],d['Name']) d['last']=map(lambda x:string.split(x)[1],d['Name']) print d print d import pandas.io.sql as pd_sql import sqlite3 as sql # sqlite3 is built into Python con = sql.connect("example_1.db") pd_sql.write_frame(d,'data',con) # write to DB as table named "data" con.close()