In csv, for a column there is ambiguity in string. Because of that, I'm getting 6
values in list instead of 5
values as output.
Code:
import csv
csv_data = csv.reader(file('test.csv'))
for row in csv_data:
print row
I tried to replace "
with space
to get atleast as normal string with out any quotes, as shown below,
for row in csv_data:
print [r.replace('"',' ') for r in row] # This did't worked as expected.
Input:
row in csv file looks like this,
1,2,"text1", "Sample text ""present" in csv, as this",5
"Sample text "present" in csv, as this" # Error due to this value.
Output:
['1', '2', 'text1', 'Sample text present" in csv', 'as this', 5]
Expected output:
['1', '2', 'text1', 'Sample text "present" in csv, as this', 5]
This is almost embarrassingly hacky, but seems to work at least on the sample input shown in your question. It works by post-processing each row read by the
csvreader
and tries to detect when they have been read incorrectly due to the bad formatting — and then corrects it.Output: