Python的Unicode使用
在做数据处理时,通常会遇到Unicode的问题。 这里对python的编码问题做简单的总结。
基础
一个字符character不是一个字节byte
unicode是一个字符数据库,每个字符对应的唯一数字被称为code point
字符编码encoding定义字符和字节表示之间的映射
python的几类字符
- 原始字符串操作符(r/R)
- Unicode字符串操作符(u/U)
- 特殊字符和控制字符
- Unicode
python的三种字符类型定义
- ‘unicode’代表unicode strings (text strings)
- ‘str’ 代表 byte strings (binary data)
- ‘basestring’, str和unicode的父类
python默认编码ASCII,该编码被用到str()和unicode()及任何涉及转换到字符型的函数上
用sys.setdefaultencoding()修改默认编码只是掩盖了问题.
正确的解决方式:
- All text strings, everywhere should be of type unicode, not str. If you’re handling text, and your variable is a str, it’s a bug!
- To decode a byte string as text, use var.decode(encoding) (eg, var.decode(‘utf-8’), with the correct encoding. To encode a text string as bytes, use var.encode(encoding).
- Never ever use str() on a unicode string, or unicode() on a byte string without a second argument specifying the encoding.
- Whenever you read data from outside your app, expect it to be bytes - eg, of type str - and call .decode() on it to interpret it as text. Likewise, always call .encode() on text you want to send to the outside world.
- If a string literal in your code is intended to represent text, it should always be prefixed with ‘u’. In fact, you probably never want to define a raw string literal in your code at all.
str是字节串,由unicode经过编码(encode)后的字节组成的
unicode才是真正意义上的字符串,由字符组成
str.decode/unicode.encode
大致处理步骤
- 外部输入编码,decode转成unicode
- 处理(内部编码,统一unicode)
- encode转成需要的目标编码
py文件默认编码是ASCII
# -*- coding: utf-8 -*-
#coding=utf-8
系统默认编码
import sys
sys.getdefaultencoding()
# 不推荐使用sys.setdefaultencoding()`
文件编码检测
import chardet
chardet.detect(...)
Console/控制台
Python使用用户的locale (Linux/OS X/Un*x) 或 codepage (Windows) 设置该值.
sys.stdout.encoding
PYTHONIOENCODING="UTF-8"
中文使用
unicode
# 中文转Unicode编码
s= '短信'.encode("unicode_escape")
s.decode('unicode_escape')
# Unicode编码转中文
print('\u77ED\u4FE1')
# \\u形式的转换为中文
'\\u77ED\\u4FE1'.encode('utf-8').decode('unicode_escape')