python乱码小记
来源:互联网 发布:请叫我威廉三世知轩 编辑:程序博客网 时间:2024/06/04 07:42
ipython中直接print r.text会出现输出结果诸如:['\xe4\xbd\xa0\xe5\xa5\xbd', '\xe5\x93\x88\xe5\x93\x88', '\xe5\x91\xb5\xe5\x91\xb5']
因为默认的编码为utf-8,所以要将中文显示,需要你需要:
r.text.decode('utf-8').encode('gbk')##先转码成unicode,再转码成gbk输出但,此时你还是会失败,所以:
首先你需要执行:
import sysreload(sys)sys.setdefaultencoding("utf-8")
否则会出现:
In [15]: print r.text.decode('utf-8').encode('gbk')---------------------------------------------------------------------------UnicodeEncodeError Traceback (most recent call last)<ipython-input-15-ee68b5387e3d> in <module>()----> 1 print r.text.decode('utf-8').encode('gbk')C:\Python27\lib\encodings\utf_8.pyc in decode(input, errors) 14 15 def decode(input, errors='strict'):---> 16 return codecs.utf_8_decode(input, errors, True) 17 18 class IncrementalEncoder(codecs.IncrementalEncoder):UnicodeEncodeError: 'ascii' codec can't encode characters in position 77-78: ordinal not in range(128)
几天,装scrapy又遇到乱码坑货问题了。
先是用easy_install 装,遇到vc++需要手工安装,然后又是libssl-dev装不上,搜了半天win下好像也没有。然后转战pip,结果又遇到如下报错,无法进行了:
<span style="font-size:18px;">UnicodeEncodeError: 'ascii' codec can't encode character u'\u2588' in position 8: ordinal not in range(128)</span>
然后这样解决了问题:在python的Lib\site-packages文件夹下新建一个sitecustomize.py,内容为:
# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')
这样系统在python启动的时候,自行调用该文件,设置系统的默认编码,而不需要每次都手动的加上解决代码,一劳永逸。
另外,pypi中很多软件已经不提供exe了。。只提供tar和whl,whl只能用pip安装,诸如:pip install xxx.whl easy_install则不行,所以以后就用pip啦!
0 0
- python乱码小记
- python 小记
- python小记
- python小记
- python 小记
- python 小记
- Python 小记
- Python小记
- Python简单的爬取网页信息并生成json文件与乱码解决小记
- Mysql乱码问题(小记)
- python 读http小记
- Python模块subprocess小记
- Python模块subprocess小记
- python基本知识小记1
- PYTHON编码转换小记
- Python模块subprocess小记
- 学习小记--Python--5.31
- 学习小记--Python--6.2
- 【HDU3068】最长回文【Manacher】
- 单点登录方案的比较和选择
- 【URAL1297】Palindrome【Manacher】
- LightOJ 1077 - How Many Points? (线段整数点个数)
- 解决中文文件名显示乱码的问题
- python乱码小记
- 11款开放中文分词引擎大比拼
- bzoj 2055 80人环游地球(上下界费用流)
- 对于支付宝进军社交的看法
- 阿里分布式数据库服务实践
- Nginx简介
- App架构设计经验谈:接口的设计
- Struts2-04-struts2工作原理
- reading note 1