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?
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– hkotsubo
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– Loip