根据输入提取字符串的特定部分

Extract a specific section of a string depending on an input

我有一个非常大的.json转换成一个string,包含无数个cities/countries。

我想根据用户选择的国家提取城市信息(伦敦只是一个例子)。

例如,如果 Country 用户 输入 是:UK,将从字符串中提取以下信息:

由于我缺乏经验,我不太确定如何实现这一点,但我知道这需要一个 if 语句。目前我的进度:

Country = raw_input('Country: ')
if 'UK' in string:
    ???
import json

country = raw_input('Country: ')

jsondata = "the large json string mentioned in your post"

info = json.loads(jsondata)

for item in info:
    if item['country'] == country:
        print item

你可以试试这个。可能仍要考虑代码中的一些用户输入错误。例如,str.strip() 和大小写敏感。

import json
input_country = raw_input('Please enter country:')
with open('London.json') as fp:
    london_json = fp.read()
    london = json.loads(london_json)
    for item in london["response"]["results"]:
        if item['country'] == input_country:
            print json.dumps(item, indent = 2)

最初的反应并不好,因为我们中的一些人忽略了原始 JSON。但是,您 确实 提供了它,因此将来最好使您显示的代码段具有更完整(和有效)的副本更加明显。

也就是说,我会将数据加载到字典中并执行如下操作:

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"

编辑: 根据评论使用 URL 而不是 JSON 字符串。

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"