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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months 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)

PHP: Handling Databases and Server-Side Logic PHP: Handling Databases and Server-Side Logic Apr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

PHP and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

PHP: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

See all articles