[Tutor] Regex Question

Leena Gupta gupta.leena at gmail.com
Mon Sep 30 22:29:00 CEST 2013


Hello,

I have a TSV file that has the city,state,country information in this
format:
Name               Display name      Code
San Jose          SJC                     SJC - SJ (POP), CA (US)
San Francisco  SFO                    SFO - SF, CA (US)

I need to extract the state and country for each city from this file. I'm
trying to do this in python by using the following Regex:

s=re.search(',(.*?)\(',text)
               if s:
                   state=s.group(1).strip()
c=re.search('\((.*?)\)',text)
               if c:
                   country=c.group(1).strip()


This works well for the state. But for country for San Jose, it brings the
following:
country = POP

I think it maybe better to search from the end of the string,but I am
unable to get the right syntax. Could you please share any pointers?

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130930/e2d76490/attachment.html>


More information about the Tutor mailing list