Python Question

csv python questions

i am opening a csv file like this:

import csv
reader = csv.reader(open("book1.csv", "rb"))
for row in reader:
print row


how can i replace the value in column 3 with its
log
and then save the result into a new csv?

Answer

Like this?

>>> input = "1,2,3\n4,5,6\n7,8,9".splitlines()
>>> reader=csv.reader(input)
>>> for row in reader:
...     row[2] = log(float(row[2]))
...     print ','.join(map(str,row))
...
1,2,1.09861228867
4,5,1.79175946923
7,8,2.19722457734
Comments