我不明白这一点,为什么你必须从 utf-8 转换。
来自unicode 文档:
UTF-8 使用以下规则:
If the code point is < 128, it’s represented by the corresponding byte value.
If the code point is >= 128, it’s turned into a sequence of two, three, or four bytes, where each byte of the sequence is between 128 and 255.
您可以将其转换为ascii,例如:
u.encode('utf-8') = b"\xea\x80\x80abcd\xde\xb4 u'\\u2019'=\xe2\x80\x99"
u.encode('ascii', 'ignore') = b"abcd u'\\u2019'="
u.encode('ascii', 'replace') = b"?abcd? u'\\u2019'=?"
u.encode('ascii', 'xmlcharrefreplace') = b"ꀀabcd޴ u'\\u2019'=’"
u.encode('ascii', 'backslashreplace') = b"\\ua000abcd\\u07b4 u'\\u2019'=\\u2019"
来自re 文档:
要搜索的模式和字符串都可以是 Unicode 字符串以及 8 位字符串。但是,Unicode 字符串和 8 位字符串不能混合使用:也就是说,您不能将 Unicode 字符串与字节模式匹配,反之亦然;同样,在请求替换时,替换字符串必须与模式和搜索字符串的类型相同。
re.A
re.ASCII
Make \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead of full Unicode matching.
This is only meaningful for Unicode patterns, and is ignored for byte patterns.
Note that for backward compatibility, the re.U flag still exists
(as well as its synonym re.UNICODE and its embedded counterpart (?u)),
but these are redundant in Python 3 since matches are Unicode by default for strings
(and Unicode matching isn’t allowed for bytes).
用 Python:3.4.2 测试