Bin*_*hen 177 python unicode utf-8 python-2.7
我有一个浏览器,它向我的Python服务器发送utf-8字符,但是当我从查询字符串中检索它时,Python返回的编码是ASCII.如何将纯字符串转换为utf-8?
注意:从Web传递的字符串已经是UTF-8编码的,我只想让Python将其视为UTF-8而不是ASCII.
use*_*312 244
>>> plain_string = "Hi!"
>>> unicode_string = u"Hi!"
>>> type(plain_string), type(unicode_string)
(<type 'str'>, <type 'unicode'>)
Run Code Online (Sandbox Code Playgroud)
^这是字节字符串(plain_string)和unicode字符串之间的区别.
>>> s = "Hello!"
>>> u = unicode(s, "utf-8")
Run Code Online (Sandbox Code Playgroud)
^转换为unicode并指定编码.
duh*_*ime 67
如果上述方法不起作用,您还可以告诉Python忽略无法转换为utf-8的字符串部分:
stringnamehere.decode('utf-8', 'ignore')
Run Code Online (Sandbox Code Playgroud)
小智 21
可能有点矫枉过正,但是当我在同一个文件中使用ascii和unicode时,重复解码会很麻烦,这就是我使用的:
def make_unicode(input):
if type(input) != unicode:
input = input.decode('utf-8')
return input
Run Code Online (Sandbox Code Playgroud)
小智 14
将以下行添加到.py文件的顶部:
# -*- coding: utf-8 -*-
Run Code Online (Sandbox Code Playgroud)
允许您直接在脚本中编码字符串,如下所示:
utfstr = "????"
Run Code Online (Sandbox Code Playgroud)
cod*_*ape 13
如果我理解正确,你的代码中有一个utf-8编码的字节串.
将字节字符串转换为unicode字符串称为解码(unicode - > byte-string is encoding).
unicodestr = unicode(bytestr, encoding)
unicodestr = unicode(bytestr, "utf-8")
Run Code Online (Sandbox Code Playgroud)
要么:
unicodestr = bytestr.decode(encoding)
unicodestr = bytestr.decode("utf-8")
Run Code Online (Sandbox Code Playgroud)
小智 9
city = 'Ribeir\xc3\xa3o Preto'
print city.decode('cp1252').encode('utf-8')
Run Code Online (Sandbox Code Playgroud)
小智 6
在Python 3.6中,它们没有内置的unicode()方法.默认情况下,字符串已存储为unicode,无需转换.例:
my_str = "\u221a25"
print(my_str)
>>> ?25
Run Code Online (Sandbox Code Playgroud)
使用 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')
ñ
Run Code Online (Sandbox Code Playgroud)
小智 5
url 被转换为 ASCII,对于 Python 服务器来说,它只是一个 Unicode 字符串,例如:\n"T%C3%A9st%C3%A3o"
\nPython 将“\xc3\xa9”和“\xc3\xa3”理解为实际的%C3%A9 和%C3%A3。
\n您可以像这样对 URL 进行编码:
\nimport urllib\nurl = "T%C3%A9st%C3%A3o"\nprint(urllib.parse.unquote(url))\n>> T\xc3\xa9st\xc3\xa3o\nRun Code Online (Sandbox Code Playgroud)\n有关详细信息,请参阅https://www.adamsmith.haus/python/answers/how-to-decode-a-utf-8-url-in-python。
\n| 归档时间: |
|
| 查看次数: |
539629 次 |
| 最近记录: |