问题
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xb5 in position 23: ordinal not in range(128)
问题说明
- 对于大多数Windows:默认编码好像都是ascii
- print sys.getdefaultencoding() #可以看下
- 因为默认ascii原因,执行就会报错
#coding: utf-8 s = '你好 中文' s.encode('utf-8')
- 原因是因为系统会在执行decode(‘utf-8’)之前加入默认的解码 其实就变成了下面的语句
#coding: utf-8 s = '你好 中文' s.decode('ascii').encode('utf-8')
问题解决
- 解决方法就是默认执行的decode(‘ascii’)改为执行decode(‘utf-8’)
#coding: utf-8 import sys #reload()之前必须要引入模块 reload(sys) sys.setdefaultencoding('utf-8') #直接执行,好像不起作用