Home > Blockchain >  Deleting the extra b after converting accented characters to ASCII
Deleting the extra b after converting accented characters to ASCII

Time:02-17

I have the following string:

my_string = 'Climate change effects on El Niño by Alberto Mesón'

And I want to convert the accented and Spanish characters to ASCII (So the expected string is "Climate change effects on El Nino by Alberto Meson". So I tried this after a solution on SO: Special text to latin characters in python

But it didn't work as expected:

unicodedata.normalize('NFKD',my_string)
'Climate change effects on El Niño by Alberto Mesón'

Then I tried this and the result was:

unicodedata.normalize('NFD', my_string).encode('ascii', 'ignore')
b'Climate change effects on El Nino by Alberto Meson'

It is the text I want, but how can I get rid of the b'?

CodePudding user response:

This is a bytestring. You can call decode() on this method to make it text string:

(unicodedata.normalize('NFD', my_string).encode('ascii', 'ignore')).decode()
  • Related