Python print unicode list

10,561
>>> import sys
>>> lst = [u'\u5de5', u'\u5de5']
>>> msg = repr([x.encode(sys.stdout.encoding) for x in lst]).decode('string-escape')
>>> print msg
['工', '工']
Share:
10,561
gongzhitaao
Author by

gongzhitaao

Lifelong researcher and programmer SOreadytohelp

Updated on June 13, 2022

Comments

  • gongzhitaao
    gongzhitaao almost 2 years

    With the following code

    lst = [u'\u5de5', u'\u5de5']
    msg = repr(lst).decode('unicode-escape')
    print msg
    

    I got

    [u'工', u'工']
    

    How can I remove the leading u so that the content of msg is:

    ['工', '工']
    
  • gongzhitaao
    gongzhitaao about 10 years
    I've got a one more question related to this. Pleas see my updated post.
  • falsetru
    falsetru about 10 years
    @gongzhitaao, Where is the one more question?
  • falsetru
    falsetru about 10 years
    @gongzhitaao, more_msg = ... just works. What is the problem?
  • gongzhitaao
    gongzhitaao about 10 years
    No, it doesn't, 'ascii' codec can't decode byte is the error message.