2

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!

JasonFitz
  • 183
  • 1
  • 11
  • 1
    Unless you want to write a state machine that keeps track of open quotes and parses character-by-character (you don't, because you want to "avoid unnecessary for loops if possible"), use the [`csv` module](https://docs.python.org/3/library/csv.html#csv.reader) – Pranav Hosangadi Apr 19 '21 at 20:27
  • You can refer to this as well https://stackoverflow.com/a/3939381/15299683 – seraph Apr 19 '21 at 20:30

1 Answers1

1

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,']
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50