2016-09-28 58 views
0

我有一个非常大的.json转换为string,其中包含许多城市/国家。根据输入提取字符串的特定部分

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

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

我不是我怎么可能做到这一点,由于我没有经验不太清楚,但我知道这需要一个if语句。到目前为止,我的进步:

Country = raw_input('Country: ') 
if 'UK' in string: 
    ??? 
+0

你知道如何使用'json'模块解析JSON吗? – FamousJameous

+0

@FamousJameous嗯,不,我会看看我猜。 – ThatOnePythonNoob

+0

以下是供将来参考的链接:https://docs.python.org/2.7/library/json.html – FamousJameous

回答

1

最初的反应是不是很大,因为我们几个人忽略了生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": "https://stackoverflow.com/q/zmw:72847.1.99999" 
     } 
     , 
     { 
     "name": "London", 
     "city": "London", 
     "state": "KY", 
     "country": "US", 
     "country_iso3166":"US", 
     "country_name":"USA", 
     "zmw": "40741.1.99999", 
     "l": "https://stackoverflow.com/q/zmw:40741.1.99999" 
     } 
     , 
     { 
     "name": "London", 
     "city": "London", 
     "state": "MN", 
     "country": "US", 
     "country_iso3166":"US", 
     "country_name":"USA", 
     "zmw": "56036.3.99999", 
     "l": "https://stackoverflow.com/q/zmw:56036.3.99999" 
     } 
     , 
     { 
     "name": "London", 
     "city": "London", 
     "state": "OH", 
     "country": "US", 
     "country_iso3166":"US", 
     "country_name":"USA", 
     "zmw": "43140.1.99999", 
     "l": "https://stackoverflow.com/q/zmw:43140.1.99999" 
     } 
     , 
     { 
     "name": "London", 
     "city": "London", 
     "state": "ON", 
     "country": "CA", 
     "country_iso3166":"CA", 
     "country_name":"Canada", 
     "zmw": "00000.1.71623", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.71623" 
     } 
     , 
     { 
     "name": "London", 
     "city": "London", 
     "state": "TX", 
     "country": "US", 
     "country_iso3166":"US", 
     "country_name":"USA", 
     "zmw": "76854.1.99999", 
     "l": "https://stackoverflow.com/q/zmw:76854.1.99999" 
     } 
     , 
     { 
     "name": "London", 
     "city": "London", 
     "state": "", 
     "country": "UK", 
     "country_iso3166":"GB", 
     "country_name":"United Kingdom", 
     "zmw": "00000.1.03772", 
     "l": "https://stackoverflow.com/q/zmw:00000.1.03772" 
     } 
     , 
     { 
     "name": "London", 
     "city": "London", 
     "state": "WV", 
     "country": "US", 
     "country_iso3166":"US", 
     "country_name":"USA", 
     "zmw": "25126.1.99999", 
     "l": "https://stackoverflow.com/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" 

编辑: 基于评论时使用的网址,而不是一个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" 
+0

老实说,谢谢你!谢谢你的耐心!代码很好用! – ThatOnePythonNoob

+1

@ThatOnePythonNoob你是非常欢迎:)我更新,以便它会拒绝不正确的国家名称。不要因以前的困难而感到灰心;就像我说的那样,您发布了一个精确的json结构,只是非常容易让底部显示正确,并在顶部显示无效的代码片段。如果此代码已解决您的问题,我将非常感谢您将其标记为正确(单击答案旁边的勾号),以便其他人不认为它仍未回答。 – roganjosh

+0

是否可以将'json_string'更改为API链接(http://api.wunderground.com/api/a8c3e5ce8970ae66/conditions/q/London.json),然后将其转换为字符串?该计划是否仍然有效? – ThatOnePythonNoob

0
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 
+0

是不是'信息['results']'中的项目? – roganjosh

+0

@John Gordon,字面上打印10000行 – ThatOnePythonNoob

+0

然后,限制它只是你想要的信息:'print item ['name'],item ['city'],item ['state']等... ' –

0

你可以试试这个。可能仍然想在代码中考虑一些用户输入错误。例如,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) 
+0

感谢您的建议!运行时,我目前得到的错误:IOError:[Errno 2]没有这样的文件或目录:'http://api.wunderground.com/api/a8c3e5ce8970ae66/conditions/q/London.json'有什么想法? – ThatOnePythonNoob

+0

'open()'只适用于本地文件,而不适用于网址。 –