String to sequence
mattia
gervaz at gmail.com
Sun Mar 15 06:37:38 EDT 2009
Il Sat, 14 Mar 2009 15:30:29 -0500, Tim Chase ha scritto:
>> 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
Yes, I'll use a regex to rule them all.
More information about the Python-list
mailing list