Removing the zip code from a python list (to obtain the state name from MapQuest output)
up vote
1
down vote
favorite
This should be simple, but could not get it to work.
I have some strings returned to me by the geolocation MapQuest API. I want to isolate the state name from strings like these, which is kind of hard. Think of 'Pennsylvania Avenue' (which is in D.C.), then there is 'Washington', which can be a state, as well as a street name, and a city.
s = "Goldman Sachs Tower, 200, West Street, Battery Park City, Manhattan Community Board 1, New York County, NYC, New York, 10282, United States of America"
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = "Casper, Natrona County, Wyoming, United States of America"
But I noticed that MapQuest writes the state name just before the zip code, near the end of the string.
To obtain the state name, this works, that is, if there is a zip code:
s = s.split(",")
s = [x.strip() for x in s]
state = s[-3]
However, when there is no zip code, as in the third string, then I get the county (Natrona County).
I tried to eliminate the zip code by:
s = s.split(",")
s = [x.strip() for x in s if 'd{5}' not in x ]
But the regex 'd{5}'
does not work - I want Wyoming, not Natrona County.
python geolocation
add a comment |
up vote
1
down vote
favorite
This should be simple, but could not get it to work.
I have some strings returned to me by the geolocation MapQuest API. I want to isolate the state name from strings like these, which is kind of hard. Think of 'Pennsylvania Avenue' (which is in D.C.), then there is 'Washington', which can be a state, as well as a street name, and a city.
s = "Goldman Sachs Tower, 200, West Street, Battery Park City, Manhattan Community Board 1, New York County, NYC, New York, 10282, United States of America"
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = "Casper, Natrona County, Wyoming, United States of America"
But I noticed that MapQuest writes the state name just before the zip code, near the end of the string.
To obtain the state name, this works, that is, if there is a zip code:
s = s.split(",")
s = [x.strip() for x in s]
state = s[-3]
However, when there is no zip code, as in the third string, then I get the county (Natrona County).
I tried to eliminate the zip code by:
s = s.split(",")
s = [x.strip() for x in s if 'd{5}' not in x ]
But the regex 'd{5}'
does not work - I want Wyoming, not Natrona County.
python geolocation
are you importing the re module? and then setting up your regex for searching etc? If not I suggest you read docs.python.org/3/library/re.html
– Jack Herer
Nov 11 at 7:52
yep, I indeedimport re
– Martien Lubberink
Nov 11 at 8:01
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This should be simple, but could not get it to work.
I have some strings returned to me by the geolocation MapQuest API. I want to isolate the state name from strings like these, which is kind of hard. Think of 'Pennsylvania Avenue' (which is in D.C.), then there is 'Washington', which can be a state, as well as a street name, and a city.
s = "Goldman Sachs Tower, 200, West Street, Battery Park City, Manhattan Community Board 1, New York County, NYC, New York, 10282, United States of America"
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = "Casper, Natrona County, Wyoming, United States of America"
But I noticed that MapQuest writes the state name just before the zip code, near the end of the string.
To obtain the state name, this works, that is, if there is a zip code:
s = s.split(",")
s = [x.strip() for x in s]
state = s[-3]
However, when there is no zip code, as in the third string, then I get the county (Natrona County).
I tried to eliminate the zip code by:
s = s.split(",")
s = [x.strip() for x in s if 'd{5}' not in x ]
But the regex 'd{5}'
does not work - I want Wyoming, not Natrona County.
python geolocation
This should be simple, but could not get it to work.
I have some strings returned to me by the geolocation MapQuest API. I want to isolate the state name from strings like these, which is kind of hard. Think of 'Pennsylvania Avenue' (which is in D.C.), then there is 'Washington', which can be a state, as well as a street name, and a city.
s = "Goldman Sachs Tower, 200, West Street, Battery Park City, Manhattan Community Board 1, New York County, NYC, New York, 10282, United States of America"
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = "Casper, Natrona County, Wyoming, United States of America"
But I noticed that MapQuest writes the state name just before the zip code, near the end of the string.
To obtain the state name, this works, that is, if there is a zip code:
s = s.split(",")
s = [x.strip() for x in s]
state = s[-3]
However, when there is no zip code, as in the third string, then I get the county (Natrona County).
I tried to eliminate the zip code by:
s = s.split(",")
s = [x.strip() for x in s if 'd{5}' not in x ]
But the regex 'd{5}'
does not work - I want Wyoming, not Natrona County.
python geolocation
python geolocation
asked Nov 11 at 7:46
Martien Lubberink
600612
600612
are you importing the re module? and then setting up your regex for searching etc? If not I suggest you read docs.python.org/3/library/re.html
– Jack Herer
Nov 11 at 7:52
yep, I indeedimport re
– Martien Lubberink
Nov 11 at 8:01
add a comment |
are you importing the re module? and then setting up your regex for searching etc? If not I suggest you read docs.python.org/3/library/re.html
– Jack Herer
Nov 11 at 7:52
yep, I indeedimport re
– Martien Lubberink
Nov 11 at 8:01
are you importing the re module? and then setting up your regex for searching etc? If not I suggest you read docs.python.org/3/library/re.html
– Jack Herer
Nov 11 at 7:52
are you importing the re module? and then setting up your regex for searching etc? If not I suggest you read docs.python.org/3/library/re.html
– Jack Herer
Nov 11 at 7:52
yep, I indeed
import re
– Martien Lubberink
Nov 11 at 8:01
yep, I indeed
import re
– Martien Lubberink
Nov 11 at 8:01
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
Use re
:
import re
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = s.split(",")
number = re.compile(r"d{5}")
s = [x.strip() for x in s if not number.search(x)]
print s
print s[-2]
output:
['9th St NW', 'Logan Circle/Shaw', 'Washington', 'District of Columbia', 'United States of America']
District of Columbia
Here is some small easy tutorial on it: regex tutorial
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
Use re
:
import re
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = s.split(",")
number = re.compile(r"d{5}")
s = [x.strip() for x in s if not number.search(x)]
print s
print s[-2]
output:
['9th St NW', 'Logan Circle/Shaw', 'Washington', 'District of Columbia', 'United States of America']
District of Columbia
Here is some small easy tutorial on it: regex tutorial
add a comment |
up vote
2
down vote
accepted
Use re
:
import re
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = s.split(",")
number = re.compile(r"d{5}")
s = [x.strip() for x in s if not number.search(x)]
print s
print s[-2]
output:
['9th St NW', 'Logan Circle/Shaw', 'Washington', 'District of Columbia', 'United States of America']
District of Columbia
Here is some small easy tutorial on it: regex tutorial
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
Use re
:
import re
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = s.split(",")
number = re.compile(r"d{5}")
s = [x.strip() for x in s if not number.search(x)]
print s
print s[-2]
output:
['9th St NW', 'Logan Circle/Shaw', 'Washington', 'District of Columbia', 'United States of America']
District of Columbia
Here is some small easy tutorial on it: regex tutorial
Use re
:
import re
s = "9th St NW, Logan Circle/Shaw, Washington, District of Columbia, 20001, United States of America"
s = s.split(",")
number = re.compile(r"d{5}")
s = [x.strip() for x in s if not number.search(x)]
print s
print s[-2]
output:
['9th St NW', 'Logan Circle/Shaw', 'Washington', 'District of Columbia', 'United States of America']
District of Columbia
Here is some small easy tutorial on it: regex tutorial
answered Nov 11 at 8:08
Dinari
1,231322
1,231322
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246792%2fremoving-the-zip-code-from-a-python-list-to-obtain-the-state-name-from-mapquest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
are you importing the re module? and then setting up your regex for searching etc? If not I suggest you read docs.python.org/3/library/re.html
– Jack Herer
Nov 11 at 7:52
yep, I indeed
import re
– Martien Lubberink
Nov 11 at 8:01