python2 的中文编码处理

84 阅读1分钟

写在前面

Python3和Python2表示字符序列的方式有所不同。

  • Python3字符序列的两种表示为byte和str。前者的实例包含原始的8位值,即原始的字节;后者的实例包括Unicode字符。
  • Python2字符序列的两种表示为str和unicode。与Python3不同的是,str实例包含原始的8位值;而unicode的实例,则包含Unicode字符。

正文

中文编码异常报错

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
# -*- coding: utf-8 -*-  
# file: example2.py  

# 这个是 str 的字符串  
s = '关关雎鸠'

# 这个是 unicode 的字符串  
u = u'关关雎鸠'

s + u  # 失败,UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)  

解决方法

string.decode('utf-8') # string--> unicode 
unicode.encode('utf-8') # unicode--> string

www.cnblogs.com/huchong/p/9…