Search code examples
pythonsplitpython-re

Split list by comma in python unless surrounded by quotes in python


If I have a string that I want to split where the commas are, how can I exclude any substring surrounded by quotes?

Ex.

Input: 'a,b,3,"d,e",f,"e,xam,p,le,"]

Output: ['a', 'b', '3', 'd,e', 'f', 'e,xam,p,le,']

using .split(',') doesn't work because it doesn't exclude items in the quotes, and I'm not sure how I would use re.split() since there can be anything inside of the quotes. There is no set length of the substring or location where a comma could be. I'm asking to try to avoid unnecessary for loops if possible!

Thanks!


Solution

  • You can take advantage of the csv module for parsing your data. As you have a string and not a file, you can use io.StringIO to obtain a file-like object.

    The code would simply be:

    import csv
    from io import StringIO
    
    
    reader = csv.reader(StringIO('a,b,3,"d,e",f,"e,xam,p,le,"'))
    out = next(reader)
    
    print(out)
    # ['a', 'b', '3', 'd,e', 'f', 'e,xam,p,le,']