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>
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>
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 </module></code>
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>
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>
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 </module></code>
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>
Output
<code>您想查询哪个城市的天气? 北京 { "weatherinfo": { "ptime": "18:00", "weather": "晴", "cityid": "101010100", "temp2": "16℃", "temp1": "-2℃", "city": "北京", "img2": "d0.gif", "img1": "n0.gif" } } </code>
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函数的返回值会包括换行符

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use
