How can I convert the following string:

'AAR','ABZ','AGA','AHO','ALC','LEI','AOC', EGC','SXF','BZR','BIQ','BLL','BHX','BLQ'

into this sequence:

['AAR','ABZ','AGA','AHO','ALC','LEI','AOC', EGC','SXF','BZR','BIQ','BLL','BHX','BLQ']

Though several other options have come through:

 >>> s = "'EGC','SXF','BZR','BIQ','BLL','BHX','BLQ'"
 >>> import re
 >>> r = re.compile("'([^']*)',?")
 >>> r.findall(s)
 ['EGC', 'SXF', 'BZR', 'BIQ', 'BLL', 'BHX', 'BLQ']

If you want to get really fancy, you can use the built-in csv parser:

 >>> import cStringIO
 >>> st = cStringIO.StringIO(s)
 >>> import csv
 >>> class SingleQuoteDialect(csv.Dialect):
 ...     quotechar = "'"
 ...     quoting = csv.QUOTE_MINIMAL
 ...     delimiter = ","
 ...     doublequote = True
 ...     escapechar = "\\"
 ...     lineterminator = '\r\n'
 ...     skipinitialspace = True
 ...
 >>> r = csv.reader(st, dialect=SingleQuoteDialect)
 >>> r.next()
 ['EGC', 'SXF', 'BZR', 'BIQ', 'BLL', 'BHX', 'BLQ']


This gives you control over how any craziness gets handled, prescribing escaping, and allowing you to stream in the data from a file if you need. However, if they're airport codes, I suspect the easy route of just using a regex will more than suffice.

-tkc







--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to