I have multiple CSV files which I need to parse in a loop to gather information.
The problem is that while they are the same format, some are delimited by ‘\t’ and others by ‘,’.
After this, I want to remove the double-quote from around the string.
Can python split via multiple possible delimiters?
At the minute, I can split the line with one by using:
f = open(filename, "r")
fields = f.readlines()
for fs in fields:
sf = fs.split('\t')
tf = [fi.strip ('"') for fi in sf]
Splitting the file like that is not a good idea: It will fail if there is a comma within one of the fields. For example (for a tab-delimited file): The line
"field1"\t"Hello, world"\t"field3"will be split into 4 fields instead of 3.Instead, you should use the
csvmodule. It contains the helpfulSnifferclass which can detect which delimiters are used in the file. The csv module will also remove the double-quotes for you.