Loïc Noest
Loïc Noest

Reputation: 125

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é'.

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")

Upvotes: 0

Views: 285

Answers (1)

Yosua
Yosua

Reputation: 421

Suppose you have string as follow:

bef = 'caf\\xc3\\xab'

To convert to 'café' you can do the following:

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

Then print(aft) should show 'café'

Upvotes: 2

Related Questions