Home  >  Q&A  >  body text

python 编码问题,求助各位大神

想请问下拿到一串unicode编码串,要怎么才能转换中文输出

我发现直接

print u'\u4ed7\u5251\u5929\u6daf'

或者

a = u"\u4ed7\u5251\u5929\u6daf"
print a

是可以输出中文

但是如果把串赋值到变量,用decode,encode转换都不行

a = '\u4ed7\u5251\u5929\u6daf'

不知道怎样print a才能输出中文了?

黄舟黄舟2766 days ago492

reply all(4)I'll reply

  • 黄舟

    黄舟2017-04-17 17:54:27

    In Python3, all string types have been changed to unicode. You can use it as follows and the output will be the same

    # -*- coding: utf-8 -*-
    
    a = u"\u4ed7\u5251\u5929\u6daf"
    print(a, len(a), type(a))
    
    b = "\u4ed7\u5251\u5929\u6daf"
    print(b, len(b), type(b))
    
    

    In Python2.7, adding u means unicode, and not adding it means string, so you directly remove u and get an equivalent string. You can see it by printing the data type and string length respectively

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:54:27

    The last sentence
    a = 'u4ed7u5251u5929u6daf'
    a is actually already a string
    So you need to convert a to unicode to print Chinese,
    You can use
    print a.decode('unicode-escape')
    Reference
    http://stackoverflow.com/questions/10268518/python-string-to-unicode

    reply
    0
  • 黄舟

    黄舟2017-04-17 17:54:27

    The last sentence a = 'u4ed7u5251u5929u6daf' Why should we drop the u before the quotation marks?

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:54:27

    The u in front of the Python string indicates that the string should be parsed in unicode encoding.
    You use decode, which means you want to convert the string 'u4ed7u5251u5929u6daf' into unicode encoding.

    reply
    0
  • Cancelreply