简体   繁体   English

解码utf8文字python

[英]Decoding utf8 literal python

I am trying to decode strings in a list of strings, for example 'caf\\\\xc3\\\\xab' what I want if this to be 'café' .我正在尝试解码字符串列表中的字符串,例如'caf\\\\xc3\\\\xab'如果这是'café'我想要什么。

I tried some things but ran into problems.我尝试了一些东西,但遇到了问题。

when i do:当我做:

for i in range(len(words):
     words[i] = words[i].decode("utf8")

I still need to convert to byte type but how do I do this,我仍然需要转换为字节类型,但我该怎么做,

also when I do it like this I need to remove the double backslashes for this to work同样,当我这样做时,我需要删除双反斜杠才能使其正常工作

b'caf\\xc3\\xab'.decode("utf8")

Suppose you have string as follow:假设你有如下字符串:

bef = 'caf\\xc3\\xab'

To convert to ' café ' you can do the following:要转换为“ café ”,您可以执行以下操作:

aft = bef.encode().decode('unicode-escape').encode('latin1').decode('utf-8')

Then print(aft) should show 'café'然后print(aft)应该显示 'café'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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