[Tutor] Regex Question
Mark Lawrence
breamoreboy at yahoo.co.uk
Mon Sep 30 23:19:27 CEST 2013
On 30/09/2013 21:29, Leena Gupta wrote:
> 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!
>
I'd be strongly inclined to use the CSV module from the standard library
with an excel-tab dialect name, see
http://docs.python.org/3/library/csv.html#module-csv
Please try it and if you encounter any problems feel free to get back to
us, we don't bite :)
--
Cheers.
Mark Lawrence
More information about the Tutor
mailing list