Home  >  Q&A  >  body text

python - 字符串里的u'\u00a1'怎么打印成中文

现在有一个字符串,值就是:

demo = """
[u'\u2019\u2018 \u5fc5\u987b\u4e3a\u5408\u6cd5\u7684\u65e5\u671f\u683c\u5f0f\uff0c\u8bf7\u4f7f\u7528 YYYY-MM-DD \u683c\u5f0f\u3002']
"""

现在我想把字符串里的unicode表示,打印成中文。这个怎么做呢?
就是要这样的效果:

demo = '''
[’‘ 必须为合法的日期格式,请使用 YYYY-MM-DD 格式。]
'''

这两个值之间怎么转换下呢?

大家讲道理大家讲道理2740 days ago780

reply all(2)I'll reply

  • 迷茫

    迷茫2017-04-18 10:26:16

    Why is this problem again... I have seen it at least ten times QAQ

    demo.decode('unicode-escape')

    Of course, it is limited to Python 2, because this kind of string will not be encountered in Python 3.

    In fact, your problem should be at the upper level. You shouldn't get a string like this and do what you want with it. You should get the one you want directly.

    There is a library called uniout, which can solve some uXXXX display problems in Python 2.

    It is strongly recommended to use Python 3, and you will no longer have to worry about Chinese processing.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:26:16

    print(demo[0].encode('gbk').decode('gbk'))
    See if this works. If you want to display Chinese in the file, the file encoding format must be supported.

    reply
    0
  • Cancelreply