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']