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?
我有一个浏览器,它向我的Python服务器发送utf-8字符,但是当我从查询字符串中检索它时,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.
注意:来自web的字符串已经是UTF-8编码的,我只想让Python把它当作UTF-8而不是ASCII。
8 个解决方案
#1
197
>>> 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和指定的编码。
#2
53
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')
#3
15
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
else:
return input
#4
11
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 unicode function or the decode method. Either:
您可以使用unicode函数或decode方法来实现这一点。:
unicodestr = unicode(bytestr, encoding)
unicodestr = unicode(bytestr, "utf-8")
Or:
或者:
unicodestr = bytestr.decode(encoding)
unicodestr = bytestr.decode("utf-8")
#5
11
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 = "ボールト"
#6
6
city = 'Ribeir\xc3\xa3o Preto'
print city.decode('cp1252').encode('utf-8')
#7
5
In Python 3.6, they do not have a built-in unicode() function. To convert a string to unicode, simply get the unicode value of the character, and do this:
在Python 3.6中,它们没有内置的unicode()函数。要将字符串转换为unicode,只需获取字符的unicode值,并执行以下操作:
my_str = "\u221a25"
my_str = u"{}".format(my_str)
print(my_str)
>>> √25
#8
1
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字符都有一个数字,有点像索引。所以Python有一些方法可以在char和his number之间进行转换。缺点是一个n个例子。希望它可以帮助。
>>> C = 'ñ'
>>> U = C.decode('utf8')
>>> U
u'\xf1'
>>> ord(U)
241
>>> unichr(241)
u'\xf1'
>>> print unichr(241).encode('utf8')
ñ
#1
197
>>> 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和指定的编码。
#2
53
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')
#3
15
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
else:
return input
#4
11
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 unicode function or the decode method. Either:
您可以使用unicode函数或decode方法来实现这一点。:
unicodestr = unicode(bytestr, encoding)
unicodestr = unicode(bytestr, "utf-8")
Or:
或者:
unicodestr = bytestr.decode(encoding)
unicodestr = bytestr.decode("utf-8")
#5
11
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 = "ボールト"
#6
6
city = 'Ribeir\xc3\xa3o Preto'
print city.decode('cp1252').encode('utf-8')
#7
5
In Python 3.6, they do not have a built-in unicode() function. To convert a string to unicode, simply get the unicode value of the character, and do this:
在Python 3.6中,它们没有内置的unicode()函数。要将字符串转换为unicode,只需获取字符的unicode值,并执行以下操作:
my_str = "\u221a25"
my_str = u"{}".format(my_str)
print(my_str)
>>> √25
#8
1
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字符都有一个数字,有点像索引。所以Python有一些方法可以在char和his number之间进行转换。缺点是一个n个例子。希望它可以帮助。
>>> C = 'ñ'
>>> U = C.decode('utf8')
>>> U
u'\xf1'
>>> ord(U)
241
>>> unichr(241)
u'\xf1'
>>> print unichr(241).encode('utf8')
ñ