3

I'm looking for convert this string :

test = "Il était toujours temps d’étouffer l’inflation par la hausse des taux"

to this:

test = "Il était toujours temps d'étouffer l'inflation par la hausse des taux"

I mean replace the non ascii character by "real" words/letters.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
Alan CUZON
  • 43
  • 8
  • 1
    Take a look at [this](https://stackoverflow.com/questions/1342000/how-to-make-the-python-interpreter-correctly-handle-non-ascii-characters-in-stri). – Daweed Apr 20 '21 at 10:05
  • I see the topic but I don't want to remove non ascii, just to convert to real letter. I put # -*- coding: utf-8 -*- in the top of my file but it's do not work instead. – Alan CUZON Apr 20 '21 at 10:21
  • I see, then you can consider [Unidecode](https://pypi.org/project/Unidecode/) for example. – Daweed Apr 20 '21 at 10:30

1 Answers1

2
test.encode('cp1252').decode('utf-8')

I've tried this and it works. I took it from here

domiziano
  • 440
  • 3
  • 13
  • Note that this applies because the text is in fact the result of incorrectly interpreting UTF-8 bytes as cp1252. If they'd been some other form of bytes, or interpreted as some other erroneous encoding, you need to change the precise arguments involved. – ShadowRanger Apr 20 '21 at 10:38