Skip to content

Instantly share code, notes, and snippets.

@pengwk
Created July 30, 2018 13:32
Show Gist options
  • Save pengwk/43ef740c17a85cd41cb102e69b8d4a53 to your computer and use it in GitHub Desktop.
Save pengwk/43ef740c17a85cd41cb102e69b8d4a53 to your computer and use it in GitHub Desktop.
In [150]: d = u'\xe7\xac\x91\xe5\x8f\xa3\xe5\xb8\xb8\xe5\xbc\x80'
In [151]: d
Out[151]: u'\xe7\xac\x91\xe5\x8f\xa3\xe5\xb8\xb8\xe5\xbc\x80'
In [152]: d.decode('string_escape')
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-152-0f9f72da24b9> in <module>()
----> 1 d.decode('string_escape')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-11: ordinal not in range(128)
In [153]: d.encode("unicode_escape").decode('string_escape')
Out[153]: '\xe7\xac\x91\xe5\x8f\xa3\xe5\xb8\xb8\xe5\xbc\x80'
In [154]: d.encode("unicode_escape").decode('string_escape').decode('utf-8')
...:
Out[154]: u'\u7b11\u53e3\u5e38\u5f00'
In [155]: print d.encode("unicode_escape").decode('string_escape').decode('u
...: tf-8')
笑口常开
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment