- 帖子
- 85
- 精华
- 0
- 积分
- 273
- 阅读权限
- 30
- 注册时间
- 2015-12-17
- 最后登录
- 2016-5-3
|
今天一切顺利~~
关于编码的问题,自己看了一些文章,大致有一些了解,但还是有一个问题想请教crossin先生:
我看到说python内部使用unicode编码,为什么从json.loads之后,data字典里面把原来content里面的内容都加u'xxx'了呢?是因为content是json格式,python处理的时候要先自动转成unicode格式,而且还要这样显示出来吗?这样,后面要访问data里某个键对应的值的时候,就不用再在内容前面加u'xxx'了?- # -*- coding: cp936 -*-
- import urllib2
- import json
- from city import city
- cityname=raw_input("Which city's weather do you want to know?\n")
- citycode=city.get(cityname)
- print citycode
- if citycode:
- try:
- url='http://www.weather.com.cn/data/cityinfo/%s.html'%citycode
- print url
- content=urllib2.urlopen(url).read()
- print content
- data=json.loads(content)
- print data
- print type(content)
- print type(data)
- result=data['weatherinfo']
- str_temp='%s\n%s - %s'%(result['weather'],result['temp1'],result['temp2'])
- print str_temp
- except:
- print 'Query failure.'
- else:
- print "Couldn't find the city."
复制代码 |
|