Skip to content

Instantly share code, notes, and snippets.

@drewconway
Created June 30, 2009 02:44
Show Gist options
  • Save drewconway/137954 to your computer and use it in GitHub Desktop.
Save drewconway/137954 to your computer and use it in GitHub Desktop.
def write_data(data,path,new_path):
# Takes data dict and writes new data to a new file
reader=csv.reader(open(path,'U'),delimiter=',')
writer=csv.writer(open(new_path,"w"))
row_num=0
for row in reader:
if row_num<1:
# Keep ther same column headers as before, so we simply
# re-write the first row.
writer.writerow(row)
row_num+=1
else:
# For the remainder of the rows, we write the old data with the new.
# The first three cells of each row will be from the old spreadsheet,
# but the remainder will all be from the data parsed in Part 1.
player=row[0]
position=row[1]
rnd=row[2]
height=height_cleaner(data[player]['height'])
weight=data[player]['weight']
dob=data[player]['dob']
college=data[player]['college']
current_row=[player,position,rnd,height,weight,dob,college]
writer.writerow(current_row)
print "New CSV file successfully written to "+new_path
def height_cleaner(height):
# The format of the height data (stored as: feet-inches) is troublesome
# for the CSV format. When it is read into a spreadsheet program (such as Excel)
# it will convert the data into a date (e.g, 6-1 = 6/1/2009). To avoid this,
# we create a small helper function to alter the data to a new formate (feet'inches")
height=height.replace('-',"'")
return height+'"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment