编码和解码
- 编码(encode): unicode -> str
- 解码(decode): str -> unicode
Python2
Python2中字符串分为unicode和str
unicode编码成人可读的str
s = u'你好'# ors = u'\u4f60\u597d'print type(s)# <type 'unicode'>print repr(s)# u'\u4f60\u597d'print s.encode('utf-8')# 你好
str解码成电脑使用的unicode
s = '你好'print repr(s.decode('utf-8'))# u'\u4f60\u597d'
Python3
Python3中unicode和str统一为str,字节型字符串为bytes

