Maison  >  Article  >  développement back-end  >  [新人求助] python3.5 引用字典出错

[新人求助] python3.5 引用字典出错

WBOY
WBOYoriginal
2016-09-01 00:20:121444parcourir

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
</module></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
</module></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>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn