Extract a specific section of a string depending on an input
<p>I have a very large <code>.json</code> converted into a <code>string</code>, containing numerous cities/countries.</p>

<p>I'd like to extract the information of the city depending on the user's choice of country (<em>London is just an example</em>). </p>

<p>For example, if the <code>Country</code> the user <em>inputed</em> was: <code>UK</code>, the following information would be extracted from the string:</p>

<p>I'm not too sure on how I could possibly achieve this due to my inexperience, but am aware that it would require an if statement. My progress so far:</p>

<pre><code>Country = raw_input('Country: ')
if 'UK' in string:
    ???
</code></pre>

<p>The initial response wasn't great because a few of us overlooked the raw JSON. However, you <em>did</em> provide it so in future it would be better to make it more obvious that the snippet you showed had a more complete (and valid) counterpart.</p>

<p>That said, I would load the data into a dictionary and do something like:</p>

<pre><code>import json

json_string = """{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "conditions": 1
  }
        , "results": [
        {
        "name": "London",
        "city": "London",
        "state": "AR",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "72847.1.99999",
        "l": "/q/zmw:72847.1.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "KY",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "40741.1.99999",
        "l": "/q/zmw:40741.1.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "MN",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "56036.3.99999",
        "l": "/q/zmw:56036.3.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "OH",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "43140.1.99999",
        "l": "/q/zmw:43140.1.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "ON",
        "country": "CA",
        "country_iso3166":"CA",
        "country_name":"Canada",
        "zmw": "00000.1.71623",
        "l": "/q/zmw:00000.1.71623"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "TX",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "76854.1.99999",
        "l": "/q/zmw:76854.1.99999"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "",
        "country": "UK",
        "country_iso3166":"GB",
        "country_name":"United Kingdom",
        "zmw": "00000.1.03772",
        "l": "/q/zmw:00000.1.03772"
        }
        ,
        {
        "name": "London",
        "city": "London",
        "state": "WV",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "25126.1.99999",
        "l": "/q/zmw:25126.1.99999"
        }
        ]
    }
}"""

json_object = json.loads(json_string)

world_dict = {}
for item in json_object['response']['results']:
    item_country = item['country']
    in_dict = world_dict.get(item_country)
    if in_dict:
        world_dict[item_country].extend([item])
    else:
        world_dict[item_country] = [item]

country = raw_input('Country: ')

response = world_dict.get(country)
if response:
    for item in response:
        print item
else:
    print "Not a valid country"
</code></pre>

<p>EDIT:
Based on comment to use URL rather than a JSON string.</p>

<pre><code>import requests

url = 'http://api.wunderground.com/api/a8c3e5ce8970ae66/conditions/q/London.json'

data = requests.get(url).json()


world_dict = {}
for item in data['response']['results']:
    item_country = item['country']
    in_dict = world_dict.get(item_country)
    if in_dict:
        world_dict[item_country].extend([item])
    else:
        world_dict[item_country] = [item]

country = raw_input('Country: ')

response = world_dict.get(country)
if response:
    for item in response:
        print item
else:
    print "Not a valid country"
</code></pre>

python json string python-2.7 wunderground