Removing the zip code from a python list (to obtain the state name from MapQuest output)
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 'd5' not in x ]
But the regex 'd5'
does not work - I want Wyoming, not Natrona County.
python geolocation
add a comment |
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 'd5' not in x ]
But the regex 'd5'
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 |
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 'd5' not in x ]
But the regex 'd5'
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 'd5' not in x ]
But the regex 'd5'
does not work - I want Wyoming, not Natrona County.
python geolocation
python geolocation
asked Nov 11 at 7:46
Martien Lubberink
604712
604712
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
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"d5")
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 |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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"d5")
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 |
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"d5")
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 |
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"d5")
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"d5")
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,497422
1,497422
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