Saving a Dictionary in a Python File

0

I have a function that tells how many times the words appear:

def countWordExact(dataClean):

    count = {}
    dataFreq = []
    for word in dataClean.split(" "):
        if word in count:
            count[word] += 1
        else:
            count[word] = 1

    dataFreq.append(count)
    return dataFreq

I need this information to be stored in the file and for this I do:

    arq = open('novoGuarda.txt', 'w')
    pickle.dump(data, arq)
    arq.close()

data is equivalent to dataFreq . It comes from another function hence the name change. With the code above I created the file that I invoke, but keep it this way:

melancólicarÞ  KX   tivesterß  KX   distraçãorà  KX  

What am I doing wrong?

    
asked by anonymous 11.12.2018 / 01:28

2 answers

2

pickle saves the data in a binary format recognized only by Python. This causes two problems with the code you are using:

  • The program's opening mode must be "wb" . of "binary writing"

  • The file will not be readable by a human when opened as a text file

Doing with pickle , you would do so to write:

arq = open('novoGuarda.pck', 'wb')
pickle.dump(data, arq)
arq.close()

And so to read:

arq = open('novoGuarda.pck', 'rb')
data = pickle.load(arq)
arq.close()

That said, if you want to write in a .txt, you probably want it to be readable by a human.

To do so, you could do this:

# Jeito diferente de abrir o arquivo com gerenciador de contexto, mais seguro
with open('novoGuarda.txt', 'w') as arq:
    # Se data é uma lista com somente um dict, como na função
    for key, value in data[0].items():
        arq.write(f'{key} - {value}\n')

This makes the file readable to a human, but harder to load back into another program. To have both, I recommend that you create a JSON file:

import json

...

with open('novoGuarda.json', 'w') as arq:
    json.dump(data, arq)

And to load it back:

with open('novoGuarda.json', 'r') as arq:
    data = json.load(arq)
    
11.12.2018 / 01:50
-1

Try to add encode(encoding='UTF-8') to line arq = open('novoGuarda.txt', 'w') , thus:

arq = open('novoGuarda.txt', 'w').encode(encoding='UTF-8')
    
11.12.2018 / 01:38