python - do column operation between two csv files -
i have 2 csv files this:
file1:
# full part 1 30 10 file2:
# full part 1 32 15 i want column operation , output this:
listname b diff(b-a) full 30 32 2 part 10 15 5 how can in python?
your question hard understand, can take guess @ mean:
csvdiff.py:
import csv open('file1') file1, open('file2') file2, open('output', 'w') output: next(file1), next(file2) # skip header lines c1, c2 = csv.reader(file1, delimiter=' '), csv.reader(file2, delimiter=' ') c3 = csv.writer(output, delimiter=' ') c3.writerow(['listname', 'a', 'b', 'diff(b-a)']) (row1, row2) in zip(c1, c2): c3.writerow(('full', row1[1], row2[1], int(row2[1])-int(row1[1]))) c3.writerow(('part', row1[2], row2[2], int(row2[2])-int(row1[2]))) file1:
full part 1 30 10 file2:
full part 1 32 15 output:
listname b diff(b-a) full 30 32 2 part 10 15 5 is wanted?
notice columns in test input aren't "lined up". if original data lined up, using tabs, in case you'll want use delimiter='\t' instead of delimiter=' '. if have varying numbers of spaces (as in pasted question), want leave delimiter=' ', , add skipinitialspace=true. see the docs details on of formatting options available.
and if want output lined up… easiest way use string formatting force values fixed width before printing them. example, instead of c3.writerow(…) c3.writerow('{:<20}'.format(col) col in …). if need really fancy—like figuring out right width each column going through rows , finding max width of each column—you want put kind of wrapper around writer (or maybe not use csv @ output).
there couple of tricks here:
first, use csv module handle parsing (and creating) csv files you. easy way dictreader , dictwriter (that way, don't need skip header row, , can refer columns names instead of index), since have 2 column headers 3 columns, doesn't work, need use reader , writer.
second, csv.reader file, list, or other iterable. it's sequence of rows. so, if want go through 2 csv readers in lockstep, zip them together, , single sequence of pairs of rows. (if you're using python 2.x, may want use itertools.izip instead.)
finally, csv files don't indicate types of values, reader gives strings. if know values integers, , want treat them way, have call int on them.
Comments
Post a Comment