我想將中文字符轉換爲unicode格式,如'\ uXXXX' ,但是當我使用str.encode('utf-16be')時,它會顯示:utf-8轉換爲utf-16
b'\xOO\xOO'
所以,我寫一些代碼來執行我的要求如下:
data="index=索引?"
print(data.encode('UTF-16LE'))
def convert(s):
returnCode=[]
temp=''
for n in s.encode('utf-16be'):
if temp=='':
if str.replace(hex(n),'0x','')=='0':
temp='00'
continue
temp+=str.replace(hex(n),'0x','')
else:
returnCode.append(temp+str.replace(hex(n),'0x',''))
temp=''
return returnCode
print(convert(data))
能有人給我建議做在python 3.x的這種轉換?
什麼是你定義的字符串的文件的編碼? – Kimvais
不確定是什麼問題。 UTF-16LE不是Unicode,但它是微軟稱之爲「Unicode」的東西。描述你的目標,而不是你的過程。 –
'「index =索引?」。encode('utf-16be')'給出'b'\ x00i \ x00n \ x00n \ x00d \ x00e \ x00x \ x00 =}「_ \ x15 \ x00? – lvc