2016-09-28 48 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()'只適用於本地文件,而不適用於網址。 –