如何在 Python 中将字符串转换为 utf-8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4182603/
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
How to convert a string to utf-8 in Python
提问by Bin Chen
I have a browser which sends utf-8 characters to my Python server, but when I retrieve it from the query string, the encoding that Python returns is ASCII. How can I convert the plain string to utf-8?
我有一个浏览器将 utf-8 字符发送到我的 Python 服务器,但是当我从查询字符串中检索它时,Python 返回的编码是 ASCII。如何将纯字符串转换为 utf-8?
NOTE: The string passed from the web is already UTF-8 encoded, I just want to make Python to treat it as UTF-8 not ASCII.
注意:从网络传递过来的字符串已经是 UTF-8 编码的,我只是想让 Python 把它当作 UTF-8 而不是 ASCII。
采纳答案by user225312
>>> plain_string = "Hi!"
>>> unicode_string = u"Hi!"
>>> type(plain_string), type(unicode_string)
(<type 'str'>, <type 'unicode'>)
^ This is the difference between a byte string (plain_string) and a unicode string.
^ 这是字节串 (plain_string) 和 unicode 串的区别。
>>> s = "Hello!"
>>> u = unicode(s, "utf-8")
^ Converting to unicode and specifying the encoding.
^ 转换为 unicode 并指定编码。
回答by codeape
If I understand you correctly, you have a utf-8 encoded byte-string in your code.
如果我理解正确,您的代码中有一个 utf-8 编码的字节字符串。
Converting a byte-string to a unicode string is known as decoding (unicode -> byte-string is encoding).
将字节字符串转换为 unicode 字符串称为解码(unicode -> 字节字符串是编码)。
You do that by using the unicodefunction or the decodemethod. Either:
您可以通过使用unicode函数或decode方法来做到这一点。任何一个:
unicodestr = unicode(bytestr, encoding)
unicodestr = unicode(bytestr, "utf-8")
Or:
或者:
unicodestr = bytestr.decode(encoding)
unicodestr = bytestr.decode("utf-8")
回答by duhaime
If the methods above don't work, you can also tell Python to ignore portions of a string that it can't convert to utf-8:
如果上述方法不起作用,您还可以告诉 Python 忽略无法转换为 utf-8 的字符串部分:
stringnamehere.decode('utf-8', 'ignore')
回答by Ken
Adding the following line to the top of your .py file:
将以下行添加到 .py 文件的顶部:
# -*- coding: utf-8 -*-
allows you to encode strings directly in your script, like this:
允许您直接在脚本中编码字符串,如下所示:
utfstr = "ボールト"
回答by Blueswannabe
Might be a bit overkill, but when I work with ascii and unicode in same files, repeating decode can be a pain, this is what I use:
可能有点矫枉过正,但是当我在同一个文件中使用 ascii 和 unicode 时,重复解码可能会很痛苦,这就是我使用的:
def make_unicode(input):
if type(input) != unicode:
input = input.decode('utf-8')
return input
回答by Zld Productions
In Python 3.6, they do not have a built-in unicode() method. Strings are already stored as unicode by default and no conversion is required. Example:
在 Python 3.6 中,它们没有内置的 unicode() 方法。默认情况下,字符串已存储为 unicode,无需转换。例子:
my_str = "\u221a25"
print(my_str)
>>> √25
回答by Willem
city = 'Ribeir\xc3\xa3o Preto'
print city.decode('cp1252').encode('utf-8')
回答by Joe9008
Translate with ord() and unichar(). Every unicode char have a number asociated, something like an index. So Python have a few methods to translate between a char and his number. Downside is a ? example. Hope it can help.
使用 ord() 和 unichar() 进行翻译。每个 unicode char 都有一个关联的数字,类似于索引。所以 Python 有一些方法可以在字符和他的数字之间进行转换。缺点是 ? 例子。希望它能有所帮助。
>>> C = '?'
>>> U = C.decode('utf8')
>>> U
u'\xf1'
>>> ord(U)
241
>>> unichr(241)
u'\xf1'
>>> print unichr(241).encode('utf8')
?
回答by David-Star
Yes, You can add
是的,您可以添加
# -*- coding: utf-8 -*-
in your source code's first line.
在源代码的第一行。
You can read more details here https://www.python.org/dev/peps/pep-0263/
您可以在此处阅读更多详细信息https://www.python.org/dev/peps/pep-0263/

