Python 2.7中的西里尔字母 [英] Cyrillic characters in Python 2.7
问题描述
该函数使用Google Places API返回半径范围内的位置.确切地说,我使用此库来处理任务.
The function returns places in radius using the Google Places API. To be exact, I use this library to handle the task.
问题在于,西里尔字母显示如下:
The problem is that cyrillic symbols are shown like this:
,,,, отделение ––14
ÐО Сбербанк РоÑÑии, КиевÑкое отделение â„–14
我尝试了这些建议.我也尝试过这个:
I tried these suggestions. I also tried this:
pname = place.name
uni = unicode(place.name)
这:
convertedname = pname.encode(encoding='UTF-8', errors='strict')
没有任何帮助.我还能尝试什么?
Nothing helped. What else can I try?
推荐答案
list(bytearray(надра"))
list(bytearray("надра"))
[208,189,208,176,208,180,209,128,208,176]
[208, 189, 208, 176, 208, 180, 209, 128, 208, 176]
那是UTF-8.如果您的输出终端设置为UTF-8,则基本上应该根本不需要编码或解码.但是,读取该字符串的正确方法是使用string.decode('utf-8')
将其转换为正确的Unicode字符串,然后在将其编码为输出之前,将其编码为终端支持的任何编码(看起来像... iso-8859-2 ?).
That's UTF-8. If your output terminal is set up for UTF-8, you should basically need no encoding or decoding at all. But the proper way to read that string is to use string.decode('utf-8')
to turn it into a proper Unicode string, then encode it before output to whatever encoding your terminal supports (looks vaguely like ... code page 1250 or iso-8859-2?).
https://tripleee.github.io/8bit#0xd0 显示208(0xD0)被以六种不同的编码映射到,,因此我推测您正在使用其中一种.剩下的就是我的猜测.
https://tripleee.github.io/8bit#0xd0 shows 208 (0xD0) being mapped to Đ in six different encodings, so I conjecture that you are using one of those. The rest is speculation on my part.
基本上,
pname=place.name.decode('utf-8')
显然,您还需要将其编码为适合控制台的某种输出编码,或者将控制台设置为正确支持UTF-8.如果确实您的终端当前已设置为cp1250,则它根本不支持西里尔文输出.
Apparently, you also need to encode it to some suitable output encoding for your console, or set up your console to properly support UTF-8. If indeed your terminal is presently set up for cp1250, it does not support Cyrillic output at all.
这篇关于Python 2.7中的西里尔字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!