[Newbie asking for help] python3.5 error in quoting dictionary

WBOY
Release: 2016-09-01 00:20:12
Original
1474 people have browsed it

1. I included the city number corresponding to the city name in a city.py file, and used this number to query the data on the China Weather Network
Part of the data format is as follows:

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

Below is my 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>
Copy after login
Copy after login

Running error says:

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

Which step went wrong?

Reply content:

1. I included the city number corresponding to the city name in a city.py file, and used this number to query the data on the China Weather Network
Part of the data format is as follows:

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

Below is my 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>
Copy after login
Copy after login

Running error says:

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

Which step went wrong?

The error of

EOFError is because it encountered an unexpected ending. This is considered a bug of sublime. If you want to solve it, you can read here

Running Python interactively from within Sublime Text 2

In addition, it is recommended that you use requests, urllibx series are too cheating, I changed your code and it is basically usable

<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>
Copy after login

Output

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

Are you using sunlime text to edit code? You can try your code using IDLE or pycharm to see if you can get results.

Try changing the input function to sys.stdin.readline()

<code class="python">import sys
city = sys.stdin.readline()[:-1:] # readline函数的返回值会包括换行符</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!