I am trying to read an incorrectly formatted .csv file using Python's csv module. The messy CSV looks like this:
"name","age","place","date"
"Jack","23","perth, australia","12aug
"Jackie","44","delhi, india","9dec
"Neel","12","austin, texas","1aug
"David","77","fresno, ca","21june
You'll notice that the second double quote is missing at the end of each row (the date column).
When I try to trivially read in this file using the following code, I get:
import csv
import os
temp = csv.reader(open('/Desktop/test csv/quote_test.csv', "r"), delimiter=',')
for row in temp:
print(row)
['name', 'age', 'place', 'date']
['Jack', '23', 'perth, australia', '12aug\nJackie"', '44', 'delhi, india', '9dec\nNeel"', '12', 'austin, texas', '1aug\nDavid"', '77', 'fresno, ca', '21june']
Which was expected because it reads the last element of the current row and the first element of the next row (until it finds a ", matching the last ,") as one single entry - skipping all newline chars.
My question:- Is there a better way to deal with such messy csv's in Python, where we can get at least the right number of expected columns for every row (that is, in this case, is there a way to get python to still account for the newline character), and then deal with entries like <"12aug>, <"9dec> etc. separately? If not, what other ways can one make better sense of such data using python?