Read a txt avoiding problems with python accents

Asked

Viewed 954 times

0

I’m trying to read a txt file but using the function below to display colors in the terminal and whenever the file has accents it returns me all messed up.

COLORS = {\
"black":"\u001b[30;1m",
"negrito":"\u001b[1m",
"reset":"\u001b[0m",
"red": "\u001b[31;1m",
"green":"\u001b[32m",
"yellow":"\u001b[33;1m",
"blue":"\u001b[34;1m",
"magenta":"\u001b[35m",
"cyan": "\u001b[36m",
"white":"\u001b[37m",
"brown":"\u001b[94m",
"yellow-background":"\u001b[43m",
"black-background":"\u001b[40m",
"cyan-background":"\u001b[46;1m",
}

def colorText(text):
    for color in COLORS:
        text = text.replace("[[" + color + "]]", COLORS[color])
    return text

def lertxt() :
    f = open("olamundo.txt", "r")
    arquivo = "".join(f.readlines())
    print(colorText(arquivo))
    f.close()
    return

lertxt()

with txt content being

"Hello world"

he returns me

"world"

I did some research, and I think it’s a coding problem, but I don’t know where how to force the correct encoding, how to solve?

  • 1

    It is in itself open, something like that: open('olamundo.txt', encoding='utf-8', mode='r') - obviously, changing the encoding to the one in which the file is

  • put and stayed like this: f = open("olamundo.txt", encoding="utf-8", mode="r") but it still doesn’t work, I think as much as it uses the open with the encoding utf-8 the change of the Ncode is occurring at the time that is called the colorText function

2 answers

0

You can change the text variable to something like:

string = u'N\ze3o'.encode('utf-8')

Or else, you’ve tried using .decode('latin_1') ?

It worked for me!

  • then but in my case the problem is where to insert the command for it to force the correct encoding, since I cannot insert in the print function (lertxt) because it will return to the replace function (colorText) not only a string value but also a byte value and this will summarize in an error

0


I was able to find the functional solution and in this case it would be to import IO and edit the open command line:

import io

f= io.open("olamundo.txt", "r", encoding="utf8")

For those who want to better understand the module io can read more about in https://docs.python.org/3/library/io.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.