Copying one column of a CSV file and adding it to another file using python -


i have 2 files, first 1 called book1.csv, , looks this:

 header1,header2,header3,header4,header5  1,2,3,4,5  1,2,3,4,5  1,2,3,4,5 

the second file called book2.csv, , looks this:

 header1,header2,header3,header4,header5  1,2,3,4  1,2,3,4  1,2,3,4 

my goal copy column contains 5's in book1.csv corresponding column in book2.csv.

the problem code seems not appending right nor selecting index want copy.it gives error have selected incorrect index position. output follows:

 header1,header2,header3,header4,header5  1,2,3,4  1,2,3,4  1,2,3,41,2,3,4,5 

here code:

 import csv   open('c:/users/sam/desktop/book2.csv','a') csvout:     write=csv.writer(csvout, delimiter=',')     open('c:/users/sam/desktop/book1.csv','rb') csvfile1:         read=csv.reader(csvfile1, delimiter=',')         header=next(read)         row in read:             row[5]=write.writerow(row) 

what should append properly?

thanks help!

what this. read in both books, append last element of book1 book2 row every row in book2, store in list. write contents of list new .csv file.

with open('book1.csv', 'r') book1:     open('book2.csv', 'r') book2:         reader1 = csv.reader(book1, delimiter=',')         reader2 = csv.reader(book2, delimiter=',')          both = []         fields = reader1.next() # read header row         reader2.next() # read , ignore header row         row1, row2 in zip(reader1, reader2):             row2.append(row1[-1])             both.append(row2)          open('output.csv', 'w') output:             writer = csv.writer(output, delimiter=',')             writer.writerow(fields) # write header row             writer.writerows(both) 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -