1

I have a csv file containing multiple columns.I have written a program to extract a subset of columns from it. here is my code:

import csv
filename='input.csv'
f = open(filename, 'rt')
reader = csv.reader(f,quotechar='"')
with open("output.csv","a+") as o:
    for row in reader:
        print (row[7] + "," + row[10] ,file=o) 

the columns that I am extracting i.e column 8 and column 11 cause a few problems as they have newlines or may have commas in them. for eg:

aaaa,"bbbb
bbbb"
cccc,"dd,ddd"

so when my program extracts these columns from input.csv it strips the quotes thus the spaces or commas that are inside the quotes are counted as new fields. any soultions?

Anuj Kulkarni
  • 137
  • 10

0 Answers0