Python: split files using mutliple split delimiters

Posted by donalmg on Stack Overflow See other posts from Stack Overflow or by donalmg
Published on 2010-06-08T16:39:58Z Indexed on 2010/06/08 16:42 UTC
Read the original article Hit count: 778

Filed under:

Hi,

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]

Any suggestions are welcome.

© Stack Overflow or respective owner

Related posts about python