首頁  >  文章  >  後端開發  >  [新人求救] python3.5 引用字典出錯

[新人求救] python3.5 引用字典出錯

WBOY
WBOY原創
2016-09-01 00:20:121445瀏覽

1.我在一份city.py 文件中收錄了城市名字對應的城市編號,並用這個編號去中國天氣網查詢數據
部分數據格式是這樣的:

<code>city = {
    '北京': '101010100',
    '海淀': '101010200',
    '朝阳': '101010300',
    '顺义': '101010400',
    '怀柔': '101010500',
    '通州': '101010600',
    '昌平': '101010700',
    '延庆': '101010800',
    '丰台': '101010900',
    '石景山': '101011000',
    '大兴': '101011100',
    '房山': '101011200',
    '密云': '101011300',
    '门头沟': '101011400',
    '平谷': '101011500',
</code>

下面是我的程式碼

<code># -*- coding:utf-8 -*-

import urllib3
import json
from city import city

cityname = input('您想查询哪个城市的天气?\n')

citycode = city.get(cityname)
if citycode:
        url = ('http://www.weather.com.cn/data/cityinfo/%s.html'%citycode)
        content = urllib3.urlopen(url).read()
        print(content)
</code>

運行報錯 說:

<code>D:\learnpy\weather                       
 python weather.py                      
杭州                                       
Traceback (most recent call last):       
  File "weather.py", line 7, in <module> 
    cityname = input()                   
EOFError
</code>

究竟哪一步出了問題呢?

回覆內容:

1.我在一份city.py 文件中收錄了城市名字對應的城市編號,並用這個編號去中國天氣網查詢數據
部分數據格式是這樣的:

<code>city = {
    '北京': '101010100',
    '海淀': '101010200',
    '朝阳': '101010300',
    '顺义': '101010400',
    '怀柔': '101010500',
    '通州': '101010600',
    '昌平': '101010700',
    '延庆': '101010800',
    '丰台': '101010900',
    '石景山': '101011000',
    '大兴': '101011100',
    '房山': '101011200',
    '密云': '101011300',
    '门头沟': '101011400',
    '平谷': '101011500',
</code>

下面是我的程式碼

<code># -*- coding:utf-8 -*-

import urllib3
import json
from city import city

cityname = input('您想查询哪个城市的天气?\n')

citycode = city.get(cityname)
if citycode:
        url = ('http://www.weather.com.cn/data/cityinfo/%s.html'%citycode)
        content = urllib3.urlopen(url).read()
        print(content)
</code>

運行報錯 說:

<code>D:\learnpy\weather                       
 python weather.py                      
杭州                                       
Traceback (most recent call last):       
  File "weather.py", line 7, in <module> 
    cityname = input()                   
EOFError
</code>

究竟哪一步出了問題呢?

EOFError的錯誤是因為遇到了不期望的結尾, 這算是一個sublime的 bug, 想解決可以看這裡

Running Python interactively from within Sublime Text 2

另外建議你用requests, urllibx系列太坑爹, 改了下你的代碼, 基本上可用

<code>import json
import requests
from city import city

cityname = input('您想查询哪个城市的天气?\n')

citycode = city[cityname]
if citycode:
    url = ('http://www.weather.com.cn/data/cityinfo/%s.html' % citycode)
    content = requests.get(url)
    string = content.text.encode(content.encoding).decode("utf-8")
    print(json.dumps(json.loads(string), ensure_ascii=False, indent=4))
    </code>

輸出

<code>您想查询哪个城市的天气?
北京
{
    "weatherinfo": {
        "ptime": "18:00",
        "weather": "晴",
        "cityid": "101010100",
        "temp2": "16℃",
        "temp1": "-2℃",
        "city": "北京",
        "img2": "d0.gif",
        "img1": "n0.gif"
    }
}
</code>

你用的是不是sunlime text編輯程式碼的?可以把你的程式碼用IDLE或pycharm之類的試試,看看能不能執行出結果。

嘗試把input函數換成sys.stdin.readline()

<code class="python">import sys
city = sys.stdin.readline()[:-1:] # readline函数的返回值会包括换行符</code>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn