简体   繁体   English

在python中显示西里尔符号

[英]Displaying cyrillic symbols in python

Let's say I have a content in Russian in a variable: 假设我在一个变量中有一个俄语内容:

msg = '<some russian text here>'
print msg 

gives me correct value but 给我正确的价值,但

print [msg]

gives me this: 给我这个:

['\xd0\x9f\xd0\xa4 "\xd0\x9a\xd0\xa2\xd0\x9f-\xd0\xa3\xd1\x80\xd0\xb0\xd0\xbb" (\xd0\x97\xd0\x90\xd0\x9e)']

How do I keep cyrillic symbols in list? 如何将西里尔字母符号保留在列表中?

You cannot do that directly, but you can get very close with pprint . 您不能直接执行此操作,但是可以使用pprint非常接近。

There is example code in https://stackoverflow.com/a/10883893/705086 https://stackoverflow.com/a/10883893/705086中有示例代码

It covers unicode type only, but can easily be adapted to utf-8 encoded str/bytes as in OP. 它仅涵盖unicode类型,但可以像OP中一样轻松地适应utf-8编码的str / bytes。

Ideally pprint should maintain the invariant that formatted/printed PDO is a valid Python expression. 理想情况下,pprint应该保持不变的观点,即格式化/打印的PDO是有效的Python表达式。 Linked code can be hacked to maintain this invariant just as well. 链接的代码也可以被黑客维护以保持不变。

You can monkey-path pprint module to maintain this invariant: 您可以使用monkey-path pprint模块来维护此不变式:

import functools, pprint

def escape(s):
    lead = ""
    if isinstance(s, unicode):
        s = s.encode("utf-8")
        lead = "u"
    return "%s\"%s\"" % (lead, s.replace("\\", "\\\\").replace("\"", "\\\""))

def patched(f):
    if hasattr(f, "_already_patched"):
        return f

    @functools.wraps(f)
    def sub(object, *args, **kwargs):
        try:
            if isinstance(object, basestring):
                return escape(object), True, False
        except Exception:
            pass
        return f(object, *args, **kwargs)

    sub._already_patched = True
    return sub

pprint._safe_repr = patched(pprint._safe_repr)

pprint.pprint([u"\N{EURO SIGN}", u"\N{EURO SIGN}".encode("utf-8")])
[u"€", "€"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM