Appending column name in csv with Python -
Appending column name in csv with Python -
i have csvs next header row: "participating_states", "number_of_participating_agencies", "population_covered", "agencies_submitting_incident_reports", "total_number_of_incidents_reported."
i need find percentages dividing 4th column sec column, i've found python script allows me that, , adds percentage 6th column:
import csv import tempfile import shutil input_file = '2000_percentage.csv' open(input_file, 'rb') f, \ tempfile.namedtemporaryfile(delete=false) out_f: reader = csv.reader(f) author = csv.writer(out_f) writer.writerow(next(reader)) row in reader: try: val1 = float(row[3]) val2 = float(row[1]) writer.writerows([row + [val1 / val2 * 100]]) except valueerror: print('there strings in file.') shutil.move(out_f.name, input_file) this script works perfectly. want add together column name column that's added in writer.writerows([row + [val1 / val2 * 100]]). column name should "reporting_percentage." i'm using python 2.7.9. thanks!
python csv
Comments
Post a Comment