python - How to perform a simple calculation in a CSV and append the results to the file -


i have csv contains 38 colums of data, want find our how is, divide column 11 column column 38 , append data tot end of each row. missing out title row of csv (row 1.)

if able snippet of code can this, able manipulate same code perform lots of similar functions.

my attempt involved editing code designed else. see below:

from collections import defaultdict  class_col = 11 data_col = 38  # read in data open('test.csv', 'r') f:     # if have header on file     # header = f.readline().strip().split(',')     data = [line.strip().split(',') line in f]  # append relevant sum end of each row row in xrange(len(data)):     data[row].append(int(class_col)/int(data_col))  # write results new csv file open('testmodified2.csv', 'w') nf:     nf.write('\n'.join(','.join(row) row in data)) 

any appreciated. smnally

import csv  open('test.csv', 'rb') old_csv:     csv_reader = csv.reader(old_csv)     open('testmodified2.csv', 'wb') new_csv:         csv_writer = csv.writer(new_csv)         i, row in enumerate(csv_reader):             if != 0:                 row.append(float(row[10]) / float(row[37]))                 csv_writer.writerow(row) 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -