[New Help] python3.5 reference dictionary Error

Source: Internet
Author: User
1. I included the city number corresponding to the city name in a city.py file, and used this number to go to China weather net to query the data
Part of the data format is this:

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

Here's my Code.

# -*- coding:utf-8 -*-import urllib3import jsonfrom city import citycityname = 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)

Run the error said:

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

Which step is the problem?

Reply content:

1. I included the city number corresponding to the city name in a city.py file, and used this number to go to China weather net to query the data
Part of the data format is this:

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

Here's my Code.

# -*- coding:utf-8 -*-import urllib3import jsonfrom city import citycityname = 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)

Run the error said:

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

Which step is the problem?

EOFErrorThe error is because encountered an unexpected end, this is a sublime bug, want to solve can see here

Running Python interactively from within Sublime Text 2

Also recommended that you use requests , urllibx series too pit Dad, changed your code, basic available

import jsonimport requestsfrom city import citycityname = 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))    

Output

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

Did you use the Sunlime text edit code? Try using your code like idle or pycharm to see if you can execute the results.

Try replacing the input function with Sys.stdin.readline ()

import syscity = sys.stdin.readline()[:-1:] # readline函数的返回值会包括换行符
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.