0

I want to write a program that takes text from a file and prints the selected lines. When printing a word containing an accented character like "ó", I get "Ã-". I'm just trying to print the word with that accented "ó" to print as it is. So just to clarify I'm trying to take "ó" and have it print as "ó". The answer is in the comments.

  • Are you using `python 3`? It works fine for me. – monk May 06 '22 at 06:34
  • 1
    Does this answer your question? [Python 3 : Converting UTF-8 unicode Hindi Literal to Unicode](https://stackoverflow.com/questions/59335416/python-3-converting-utf-8-unicode-hindi-literal-to-unicode) – monk May 06 '22 at 06:35
  • please post an example of your code – will-hedges May 06 '22 at 15:36
  • I am using python 3.10.2 specifically and it doesn't work. And my code just simply prints the line of text in the file I've tried to do other things to it but it hasn't worked. – dylan is stupid May 10 '22 at 16:20
  • The file contains data encoded as UTF-8. Most likely you are running your program on Windows, and opening the file without specifying an encoding. This can result in the file data being decoded with the wrong encoding and outputting what is known as _mojibake_. Try opening the file like this `open(myfile.txt, 'r', encoding='utf-8')`. – snakecharmerb May 10 '22 at 16:28

1 Answers1

0

Have you tried adding something like this to the beginning of your code?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

Try this thread also, maybe you find answers there: Correct way to define Python source code encoding