So i have a text file containing:
, "Hi, I am Jane",,"Thirty two"
"2", "Mr P","X","Fifty"
and I'm reading the text file into python and appending them into a list(splitting by comma not in quotes) to get:
[['','"Hi, I am Jane",'','"Thirty two"'],['"2"', '"Mr P"', '"X"', '"Fifty"']]
The file basically have four columns, and that the first line, you could see that it begins with a comma, is because there is no first field hence having ' ' in the output list is wanted. Where as in the second line, it is a complete line with four fields.
I attempted:
filename = open("mytext.txt", "r")
f = filename.readlines()
wlst = []
for line in f:
line = line.strip('\n')
line = line.split(',')
wlst.append(line)
print(wlst)
But the output shows:
[['','"Hi', ' I am Jane"', '', '"Thirty two"'],['"2"', ' "Mr P"', ' "X"', ' "Fifty"']]
The "Hi" segment has been separated with the "I am Jane" segment. I would like to know how to split by comma, ignoring the ones in the quotes? I've seen previous post on something like this, but apparently none of the solutions work for reading in text files.