0

I'm using python 2.7. I have a string that when I map to in a dictionary get's encoded. This is the string u'cn-员工股票权益' and this is the encoded version u'cn-\u5458\u5de5\u80a1\u7968\u6743\u76ca'. When put it a dictionary for example below it's getting encoded.

Python 2.7
new_dict = {}
new_dict['random_string'] = u'cn-员工股票权益'
new_dict['random_string']
u'cn-\u5458\u5de5\u80a1\u7968\u6743\u76ca'

This is how I'm trying to decode u'cn-\u5458\u5de5\u80a1\u7968\u6743\u76ca'.decode('utf-8') but I keep on getting the error UnicodeEncodeError: 'ascii' codec can't encode characters in position 3-8: ordinal not in range(128)

I would like to retrieve the original value that was placed in the dictionary and not the encoded value. Using this tool https://www.online-toolz.com/tools/text-unicode-entities-convertor.php I'm able to decode it back to the original state I wonder what algorithm they're using. I have tried a number of solutions from stack overflow however I haven't been able to achieve the desired results. Solutions I have tried include;

oma0256
  • 103
  • 4
  • 11
  • What IDE do you use? I tried this code and seems fine with encoding, characters looks the same. – rozumir Dec 31 '20 at 14:28
  • Try running this in the terminal ``` new_dict = {} new_dict['random_string'] = u'cn-员工股票权益' new_dict['random_string'] ``` – oma0256 Dec 31 '20 at 14:35
  • I'm using python 2 btw – oma0256 Dec 31 '20 at 14:35
  • 1
    It's not encoded, it's just a different representation. Do `print new_dict['random_string']` and you'll see the Chinese characters. See [this Q&A](https://stackoverflow.com/questions/6988213/using-chinese-to-build-a-dictionary-in-python) for example. – snakecharmerb Dec 31 '20 at 15:22
  • Ohhh.. okay thank you @snakecharmerb – oma0256 Dec 31 '20 at 16:04

0 Answers0