0

How can I store a Chinese value (漢字漢字漢字) in a variable?

 x = u'\u6f22\u5b57\u6f22\u5b57\u6f22\u5b57'
 x
u'\u6f22\u5b57\u6f22\u5b57\u6f22\u5b57'
 print x
漢字漢字漢字
 y = str(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)
  • 1
    `x` is already a variable, with `漢字漢字漢字` stored in it. What are you actually trying to do? – Amadan Sep 18 '19 at 08:23
  • 2
    Do you *need* to be using Python 2.7? It's vastly simpler to write correct code to deal with Unicode and non-ASCII strings in Python 3, so if you have the option, I'd suggest switching. – Blckknght Sep 18 '19 at 08:43
  • Possible duplicate of [UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)](https://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20) – Georgy Sep 18 '19 at 14:06

0 Answers0