Reputation: 937
I have a file with many lines formatted as such:
DIV ID=0X78800009 EXT="LOS ANGELES" TY=STANDARD OWN=0X74400002 ABBR=LA
I need to pull out the EXT value, but only the part in quotes. I'm currently using this:
for line in file:
if sub in line:
extlist.append([item[4:] for item in line.split() if item.startswith('EXT=')].pop())
But it only appends the "LOS" part of LOS ANGELES to idlist. I'm a little new to python, but is there a way to wrap item[4:]
in str(item[4:])
and use string functions to extract the value i need?
As a note, the text in the EXT field varies in length, they are all random city names.
Upvotes: 3
Views: 1226
Reputation: 143162
If you can be sure that there are no other double quotes in your line then this simple approach will work:
s='DIV ID=0X78800009 EXT="LOS ANGELES" TY=STANDARD OWN=0X74400002 ABBR=LA'
s.split('"')[1]
'LOS ANGELES'
Note that using a regular expression is a more flexible/robust way to find this if the above constraints don't hold.
Otherwise this is one way to solve this problem in the spirit of "Simple is better than complex." (The Zen of Python).
Upvotes: 2
Reputation: 3326
>>> import re
>>> myString = 'DIV ID=0X78800009 EXT="LOS ANGELES" TY=STANDARD OWN=0X74400002 ABBR=LA'
>>> re.search('EXT="(.+?)"',myString).group(1)
'LOS ANGELES'
Upvotes: 4