recover txt file formatted with utf-8 in python

Asked

Viewed 283 times

0

Good night! I’m having trouble recovering files. txt with python for popular lists or dictionaries, the problem is that these files do not come with uft-8 formatting. example of txt content: what is today’s date? (here comes a tab separating) askdate

python code that reads the file:

lines = open("cmds.txt", "r").readlines()

#adicionando os comandos no dicionário
for line in lines:
    line = line.replace('\n', '')#removendo os espaços
    parts = line.split("\t") #separação por tab
    dict_cmds.update({parts[0] : parts[1]})

this is populated a dictionary and if I print the dictionary then it will not recognize the "is".

  • 2

    In the open() you can already define the type of encoding, in the doc explains it better.

1 answer

0

It’s simple, just one more parameter:

    file = open('cmds.txt', 'r', encoding='utf-8')
    for f in file:
        print(f)

Browser other questions tagged

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