Removing the zip code from a python list (to obtain the state name from MapQuest output)











up vote
1
down vote

favorite
1












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.










share|improve this question






















  • 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

















up vote
1
down vote

favorite
1












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.










share|improve this question






















  • 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















up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 7:46









Martien Lubberink

590612




590612












  • 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




















  • 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


















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














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






share|improve this answer





















    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',
    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
    });


    }
    });














     

    draft saved


    draft discarded


















    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








    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






    share|improve this answer

























      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






      share|improve this answer























        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






        share|improve this answer












        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 8:08









        Or Dinari

        1,079321




        1,079321






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python