python 2.7 - I need to read individual rows in a CSV, make calculations from values in those rows (x2), and then append those rows with the new values -
i have .csv i'm attempting read individual rows from, make calculations individual values in rows, append values resulting calculations end of row. approach iterate through each row, call value, make calc value, append row. here's mu current code (at 1 point managed append calcs last row of csv row, promising incorrect, right i'm getting "'_csv.writer' object not iterable" type error):
import csv import math f = open('planet_data.csv','a+') writer = csv.writer(f) row in writer: <- loction of error if row[1] == 'planet_radius': <- bypass csv header values pass else: = int(row[1]) b = str((4/3)*math.pi*(a**3)) row.writerow(b) print row
any appreciated, i'm guessing code still need work after iterator issue resolved. also, i'm guessing there packages/modules can simplify this, point of exercise learn how manually loop through csvs.
i don't think able write changes in same file, can create new 1 way:
with open("planet_data.csv", "rb") infile, open('planet_data_2.csv', "wb") outfile: reader = csv.reader(infile) next(reader, none) # skip header writer = csv.writer(outfile) row in reader: = int(row[1]) b = str((4/3)*math.pi*(a**3)) writer.writerow(b)
i hope help!
Comments
Post a Comment