How can i parse a comma delimited string into a list (caveat)? How can i parse a comma delimited string into a list (caveat)? python python

How can i parse a comma delimited string into a list (caveat)?


It depends how complicated you want to get... do you want to allow more than one type of quoting. How about escaped quotes?

Your syntax looks very much like the common CSV file format, which is supported by the Python standard library:

import csvreader = csv.reader(['''foo, bar, "one, two", three four'''], skipinitialspace=True)for r in reader:  print r

Outputs:

['foo', 'bar', 'one, two', 'three four']

HTH!


The shlex module solution allows escaped quotes, one quote escape another, and all fancy stuff shell supports.

>>> import shlex>>> my_splitter = shlex.shlex('''foo, bar, "one, two", three four''', posix=True)>>> my_splitter.whitespace += ','>>> my_splitter.whitespace_split = True>>> print list(my_splitter)['foo', 'bar', 'one, two', 'three', 'four']

escaped quotes example:

>>> my_splitter = shlex.shlex('''"test, a",'foo,bar",baz',bar \xc3\xa4 baz''',                              posix=True) >>> my_splitter.whitespace = ',' ; my_splitter.whitespace_split = True >>> print list(my_splitter)['test, a', 'foo,bar",baz', 'bar \xc3\xa4 baz']


You may also want to consider the csv module. I haven't tried it, but it looks like your input data is closer to CSV than to shell syntax (which is what shlex parses).