8

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:

['工', '工']
8
  • What you are doing there gives me an AttributeError. Commented Mar 30, 2014 at 15:42
  • gongzhitaao, what are you trying to achieve? Commented Mar 30, 2014 at 15:44
  • @LucianU Trying to remove the leading convert the unicode code point to characters but removing the leading u. Commented Mar 30, 2014 at 15:44
  • @gongzhitaao, I was asking for the higher purpose of your code. If you just want to print the characters, it's enough to do for c in lst: print c.encode('utf-8') Commented Mar 30, 2014 at 15:47
  • @LucianU Of course not :P. I have no problem printing out them while removing the leading u. I just need the unicode characters in a tring without u. :) Commented Mar 30, 2014 at 15:49

1 Answer 1

16
>>> import sys
>>> lst = [u'\u5de5', u'\u5de5']
>>> msg = repr([x.encode(sys.stdout.encoding) for x in lst]).decode('string-escape')
>>> print msg
['工', '工']
Sign up to request clarification or add additional context in comments.

4 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.