我有一个很大的.json
转换成一个string
,包含许多城市/国家。
我想根据用户选择的国家(伦敦只是一个例子)提取城市的信息。
例如,如果Country
用户输入的是:UK
,则将从字符串中提取以下信息:
由于我的经验不足,我不太确定如何实现这一目标,但我知道这需要一个 if 语句。到目前为止我的进展:
Country = raw_input('Country: ')
if 'UK' in string:
???
我有一个很大的.json
转换成一个string
,包含许多城市/国家。
我想根据用户选择的国家(伦敦只是一个例子)提取城市的信息。
例如,如果Country
用户输入的是:UK
,则将从字符串中提取以下信息:
由于我的经验不足,我不太确定如何实现这一目标,但我知道这需要一个 if 语句。到目前为止我的进展:
Country = raw_input('Country: ')
if 'UK' in string:
???
最初的反应不是很好,因为我们中的一些人忽略了原始 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"
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)