python代码中编码转换问题

xiaoxiao2021-03-01  39

一、16进制转换为中文汉字,可通过两种方式:

1、将16进制转换为unicode,然后再转换为中文,代码如下:

s = u"\xe5\xa4\xa7\xe5\x86\x99A\xe7\x9a\x84\xe5\xbe\xae\xe5\x8d\x9a" s2 = s.encode('raw_unicode_escape') print s2

2、直接转换为中文字符,代码如下:

s = "\xe5\xa4\xa7\xe5\x86\x99A\xe7\x9a\x84\xe5\xbe\xae\xe5\x8d\x9a" print s.decode('utf-8').encode('utf-8')

二,将中文转换为16进制字符,通过repr(t)的方法直接转换,代码如下:

t = "大写A的博客" print repr(t)

 

转载请注明原文地址: https://www.6miu.com/read-3349943.html

最新回复(0)