[New person for help] python3.5 dictionary reference error

Source: Internet
Author: User
1. i'm in a city. the py file contains the city number corresponding to the city name, and uses this number to go to the China Weather Network to query some data in the format of this: {code ...} below is my code {code ...} run error: {code ...} which step is wrong? 1. I recorded the city number corresponding to the city name in a city. py file, and used this number to go to www.weather.com.cn to query the data.
Some data formats are as follows:

City = {'Beijing': '000000', 'haidian ': '000000', 'chaoyang': '000000', 'shunyi ': '000000', 'huairou ': '123', 'Tongzhou ': '123', 'changping': '123', 'yanqing ': '123', 'fengtai': '123', 'shijingshan ': '123', 'daxing ': '123', 'fangshan': '123', 'miyun ': '123', 'mentougou': '123', 'pinggu ': '123 ',

Below is my code

#-*-Coding: UTF-8-*-import urllib3import jsonfrom city import citycityname = input ('which city do you want to query weather conditions? \ N') citycode = city. get (cityname) if citycode: url = ('http: // response) content = urllib3.urlopen (url). read () print (content)

Running error:

D: \ learyy \ weather python weather. py Hangzhou Traceback (most recent call last): File "weather. py", line 7, in
  
   
Cityname = input () EOFError
  

Which step is wrong?

Reply content:

1. I recorded the city number corresponding to the city name in a city. py file, and used this number to go to www.weather.com.cn to query the data.
Some data formats are as follows:

City = {'Beijing': '000000', 'haidian ': '000000', 'chaoyang': '000000', 'shunyi ': '000000', 'huairou ': '123', 'Tongzhou ': '123', 'changping': '123', 'yanqing ': '123', 'fengtai': '123', 'shijingshan ': '123', 'daxing ': '123', 'fangshan': '123', 'miyun ': '123', 'mentougou': '123', 'pinggu ': '123 ',

Below is my code

#-*-Coding: UTF-8-*-import urllib3import jsonfrom city import citycityname = input ('which city do you want to query weather conditions? \ N') citycode = city. get (cityname) if citycode: url = ('http: // response) content = urllib3.urlopen (url). read () print (content)

Running error:

D: \ learyy \ weather python weather. py Hangzhou Traceback (most recent call last): File "weather. py", line 7, in
  
   
Cityname = input () EOFError
  

Which step is wrong?

EOFErrorThe error is caused by an unexpected ending.sublimeTo solve the bug, please refer to here

Running Python interactively from within Sublime Text 2

We recommend that you userequests,urllibxThe series is too boring. after you change your code, it is basically available.

Import jsonimport requestsfrom city import citycityname = input ('which city do you want to query the weather conditions? \ N') citycode = city [cityname] if citycode: url = ('http: // www.weather.com.cn/data/cityinfo/developers.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

Which city of the weather do you want to query? Beijing {"weatherinfo": {"ptime": "18:00", "weather": "Qing", "cityid": "101010100", "temp2": "16 ℃ ", "temp1": "-2 ℃", "city": "Beijing", "img2": "d0.gif", "img1": "n0.gif "}}

Are you using sunlime text to edit the code? You can try your code using IDLE or pycharm to see if the result can be executed.

Replace the input function with sys. stdin. readline ()

Import syscity = sys. stdin. readline () [:-1:] # the returned value of the readline function includes a line break.

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.