In python, how would one change the string 'Nelson Vel\xc3\xa1zquez' to 'Nelson Velazquez'?


The issue is with proper encoding. The data type is a string, where the string does not convert the spanish á properly and instead shows as xc3xa1. How do you convert these to show ‘Nelson Velazquez’?

example variable:

text = 'Nelson Vel\xc3\xa1zquez'
Asked By: Ryan



Using Python b string:

text = b'Nelson Velxc3xa1zquez'
result = text.decode('utf8')
#Nelson Velázquez
Answered By: AziMez

If you have the string in the title, you have a double-encoding issue. Reverse the double encoding:

>>> s = 'Nelson Vel\xc3\xa1zquez'
>>> s.encode('latin1').decode('unicode-escape').encode('latin1').decode('utf8')
'Nelson Velázquez'

Ideally, fix the problem at the source. The string was either read incorrectly in the first place, or written to storage incorrectly to begin with.

Answered By: Mark Tolonen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.