I have the following Python code. It reads a csv file and outputs the first column to a new csv file.
But the problem is in my csv file there is \n apart from new lines for creating new rows. But they are wrapped within a double quote.
For instance like this:
A, B, C,
D, "12
34", E,
F, G, H
So I think i need to add quotechar='”‘ somewhere but I couldnt figure it out.
f_in = open('source.csv')
fields = []
for line in f_in.readlines():
fields.append([item.strip('\n') for item in line.split(',')])
f_in.close()
f_out = open('sourceNew.csv', 'w')
for i in range(len(fields)):
if fields[i][0] != '':
f_out.write(fields[i][0] + ',,\n')
f_out.close()
How can i distinguish the real new row \n and \n wrapped in a double quote.
Thanks
Use the Python CSV module:
I hope this helps.