Skip to content

Instantly share code, notes, and snippets.

@miura
Created September 6, 2013 11:41
Show Gist options
  • Select an option

  • Save miura/6462680 to your computer and use it in GitHub Desktop.

Select an option

Save miura/6462680 to your computer and use it in GitHub Desktop.
a sample script for writing data to a csv file.
'''
writing data to a csv file.
'''
import os, csv
# prepare test data to wirte to a csv file
data1 = range(10)
data2 = [x * x for x in data1]
data3 = [pow(x, 3) for x in data1]
print data3
# prepare path
root = "/Users/miura/Desktop"
filename = "testdata.csv"
fullpath = os.path.join(root, filename)
print fullpath
# open the file first (if its not there, newly created)
f = open(fullpath, 'wb')
# create csv writer
writer = csv.writer(f)
# for loop to write each row
for i in range(len(data1)):
row = [data1[i], data2[i], data3[i]]
writer.writerow(row)
#writer.writerows([data1, data2, data3])
# close the file.
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment