Python 将字节字符串转换为 unicode 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13837848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting byte string in unicode string
提问by Alex T
I have a code such that:
我有这样的代码:
a = "\u0432"
b = u"\u0432"
c = b"\u0432"
d = c.decode('utf8')
print(type(a), a)
print(type(b), b)
print(type(c), c)
print(type(d), d)
And output:
和输出:
<class 'str'> в
<class 'str'> в
<class 'bytes'> b'\u0432'
<class 'str'> \u0432
Why in the latter case I see a character code, instead of the character? How I can transform Byte string to Unicode string that in case of an output I saw the character, instead of its code?
为什么在后一种情况下我看到的是字符代码,而不是字符?如何将字节字符串转换为 Unicode 字符串,以便在输出的情况下看到字符而不是其代码?
采纳答案by Lennart Regebro
In strings (or Unicode objects in Python 2), \uhas a special meaning, namely saying, "here comes a Unicode character specified by it's Unicode ID". Hence u"\u0432"will result in the character в.
在字符串(或 Python 2 中的 Unicode 对象)中,\u有一个特殊的含义,即“这里来了一个由它的 Unicode ID 指定的 Unicode 字符”。因此u"\u0432"将产生字符 в。
The b''prefix tells you this is a sequence of 8-bit bytes, and bytes object has no Unicode characters, so the \ucode has no special meaning. Hence, b"\u0432"is just the sequence of the bytes \,u,0,4,3and 2.
该b''前缀告诉你这是8位字节序列,和字节对象没有任何Unicode字符,所以该\u代码有没有特殊的含义。因此,b"\u0432"只是字节\, u, 0, 4,3和的序列2。
Essentially you have an 8-bit string containing not a Unicode character, but the specification of a Unicode character.
本质上,您有一个 8 位字符串,其中不包含 Unicode 字符,而是包含 Unicode 字符的规范。
You can convert this specification using the unicode escape encoder.
您可以使用 unicode 转义编码器转换此规范。
>>> c.decode('unicode_escape')
'в'
回答by SoothingMist
Loved Lennart's answer. It put me on the right track for solving the particular problem I had faced. What I added was the ability to produce html-compatible code for \u???? specifications in strings. Basically, only one line was needed:
喜欢伦纳特的回答。它使我走上了解决我所面临的特定问题的正确轨道。我添加的是为 \u 生成与 html 兼容的代码的能力????字符串中的规范。基本上,只需要一行:
results = results.replace('\u','&#x')
This all came about from a need to convert JSON results to something that displays well in a browser. Here is some test code that is integrated with a cloud application:
这一切都源于需要将 JSON 结果转换为在浏览器中显示良好的内容。下面是一些与云应用程序集成的测试代码:
# References:
# http://stackoverflow.com/questions/9746303/how-do-i-send-a-post-request-as-a-json
# https://docs.python.org/3/library/http.client.html
# http://docs.python-requests.org/en/v0.10.7/user/quickstart/#custom-headers
# http://stackoverflow.com/questions/606191/convert-bytes-to-a-python-string
# http://www.w3schools.com/charsets/ref_utf_punctuation.asp
# http://stackoverflow.com/questions/13837848/converting-byte-string-in-unicode-string
import urllib.request
import json
body = [ { "query": "co-development and language.name:English", "page": 1, "pageSize": 100 } ]
myurl = "https://core.ac.uk:443/api-v2/articles/search?metadata=true&fulltext=false&citations=false&similar=false&duplicate=false&urls=true&extractedUrls=false&faithfulMetadata=false&apiKey=SZYoqzk0Vx5QiEATgBPw1b842uypeXUv"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(body)
jsondatabytes = jsondata.encode('utf-8') # needs to be bytes
req.add_header('Content-Length', len(jsondatabytes))
print ('\n', jsondatabytes, '\n')
response = urllib.request.urlopen(req, jsondatabytes)
results = response.read()
results = results.decode('utf-8')
results = results.replace('\u','&#x') # produces html hex version of \u???? unicode characters
print(results)

