简体   繁体   中英

Print a Chinese character in Jupyter notebook (Python 3)

I am trying to print the character 維 in a Jupyter notebook. I can do it using the following code

w = '\xe7\xb6\xad'

print( bytes(w, encoding='raw-unicode-escape').decode('utf-8') )

However, I only have the string z where

z = '\\xe7\\xb6\\xad'

I have tried the following solutions from other questions on SO.

print( bytes(z, encoding='raw-unicode-escape').decode('utf-8') )

print( z.encode('utf-8').decode('unicode_escape') )

import codecs
u = codecs.decode(z, 'raw-unicode-escape').encode('utf-8')
print(u)

print( str(u.decode('utf-8')) )

bytes( str(u.decode('utf-8')), encoding='raw-unicode-escape').decode('utf-8')

None of them prints 維.

How can I print 維 if I only start with z ?

我找到了一个方法

bytes.fromhex(z.replace('\\x', '')).decode('utf-8')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM