Thank you all, this is great.

Kent Johnson wrote:
On Tue, Feb 24, 2009 at 6:48 AM, Norman Khine <nor...@khine.net> wrote:
Hello,
From my previous post on create dictionary from csv, i have broken the
problem further and wanted the lists feedback if it could be done better:

s = 'Association of British Travel Agents (ABTA) No. 56542\nAir Travel
Organisation Licence (ATOL)\nAppointed Agents of IATA (IATA)\nIncentive
Travel & Meet. Association (ITMA)'
licences = re.split("\n+", s)
licence_list = [re.split("\((\w+)\)", licence) for licence in licences]

This is awkward. You can match directly on what you want:

In [7]: import re

In [8]: s = 'Association of British Travel Agents (ABTA) No.
56542\nAir Travel Organisation Licence (ATOL)\nAppointed Agents of
IATA (IATA)\nIncentive Travel & Meet. Association (ITMA)'

In [9]: licenses = re.split("\n+", s)

In [10]: licenseRe = re.compile(r'\(([A-Z]+)\)( No. (\d+))?')

In [11]: for license in licenses:
   ....:     m = licenseRe.search(license)
   ....:     print m.group(1, 3)

('ABTA', '56542')
('ATOL', None)
('IATA', None)
('ITMA', None)

Kent

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to