Table of Contents
Reply content:
Home Backend Development PHP Tutorial [Newbie asking for help] python3.5 error in quoting dictionary

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

Sep 01, 2016 am 12:20 AM
php python

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()

import sys
city = sys.stdin.readline()[:-1:] # readline函数的返回值会包括换行符
Copy after login
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
The Continued Use of PHP: Reasons for Its Endurance The Continued Use of PHP: Reasons for Its Endurance Apr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

Python vs. C  : Learning Curves and Ease of Use Python vs. C : Learning Curves and Ease of Use Apr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. JavaScript: Development Environments and Tools Python vs. JavaScript: Development Environments and Tools Apr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Golang vs. Python: The Pros and Cons Golang vs. Python: The Pros and Cons Apr 21, 2025 am 12:17 AM

Golangisidealforbuildingscalablesystemsduetoitsefficiencyandconcurrency,whilePythonexcelsinquickscriptinganddataanalysisduetoitssimplicityandvastecosystem.Golang'sdesignencouragesclean,readablecodeanditsgoroutinesenableefficientconcurrentoperations,t

Laravel vs. Python (with Frameworks): A Comparative Analysis Laravel vs. Python (with Frameworks): A Comparative Analysis Apr 21, 2025 am 12:15 AM

Laravel is suitable for projects that teams are familiar with PHP and require rich features, while Python frameworks depend on project requirements. 1.Laravel provides elegant syntax and rich features, suitable for projects that require rapid development and flexibility. 2. Django is suitable for complex applications because of its "battery inclusion" concept. 3.Flask is suitable for fast prototypes and small projects, providing great flexibility.

Python vs. C  : Understanding the Key Differences Python vs. C : Understanding the Key Differences Apr 21, 2025 am 12:18 AM

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? In back-end development, how to distinguish the responsibilities of the service layer and the dao layer? Apr 19, 2025 pm 01:51 PM

Discussing the hierarchical architecture in back-end development. In back-end development, hierarchical architecture is a common design pattern, usually including controller, service and dao three layers...

See all articles