我有一个将 utf-8 字符发送到我的 Python 服务器的浏览器,但是当我从查询字符串中检索它时,Python 返回的编码是 ASCII。如何将纯字符串转换为 utf-8?
注意:从网络传递过来的字符串已经是 UTF-8 编码的,我只是想让 Python 将其视为 UTF-8 而不是 ASCII。
最佳答案
>>> plain_string = "Hi!"
>>> unicode_string = u"Hi!"
>>> type(plain_string), type(unicode_string)
(<type 'str'>, <type 'unicode'>)
^ 这是字节字符串(plain_string)和unicode字符串的区别。
>>> s = "Hello!"
>>> u = unicode(s, "utf-8")
^ 转换为 unicode 并指定编码。
所有字符串都是 unicode。 unicode
函数不再存在。查看@Noumenon 的回答
https://stackoverflow.com/questions/4182603/