0

I encounter a lot of encoding and decoding problems so I read a lot about it but I just can't get a grip on it. The logic just confuses me and escapes me so I'm kind of lost. To extend my knowledge and read through: https://realpython.com/python-encodings-guide/#enter-unicode

There is this example which I executed but I get another result as on the website:

import requests

from bs4 import BeautifulSoup

print("résumé".encode("utf-8"))
# b'r\xc3\xa9sum\xc3\xa9'
print("El Niño".encode("utf-8"))
# b'El Ni\xc3\xb1o'

print(b"r\xc3\xa9sum\xc3\xa9".decode("utf-8"))
# 'r�sum�'
print(b"El Ni\xc3\xb1o".decode("utf-8"))
# 'El Ni�o'

As you can see, the decoding gives me the diamond shape characters with a question mark. The correct result should be: 'résumé' 'El Niño' What is happening here? What mistakes do I make?

I use Visual Studio Code. Python version 3.12.2. Beautifulsoup version 4.12.3. I'm on Windows 11. Files encoding is set to: Files encoding setting

8
  • 2
    From what you've shown here, your code and your editor are fine. Your terminal, though, would appear to be using a font that does't have a é or ñ character to display. Commented Feb 28, 2024 at 22:02
  • 1
    (If it were an encoding issue in your terminal, each of the non-ASCII characters would be displayed as two question-mark glyphs, one for each byte in the UTF-8 encoding of the single Unicode character.) Commented Feb 28, 2024 at 22:04
  • This seems to work for me as expected print("résumé".encode("utf-8").decode("utf-8")) Commented Feb 28, 2024 at 22:24
  • Note that the default is "utf-8" so you can also just do: print("résumé".encode().decode()) Commented Feb 28, 2024 at 22:27
  • Thanks for replying. That said, the suggestions given by JonSG seems to work for him but not for me. When I execute both lines of code they give me these results: r�sum� r�sum�. Perhaps a terminal issue on my side a chepner pointed out. Commented Feb 28, 2024 at 23:00

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.