Store a dictionary in a Python file

Asked

Viewed 414 times

0

I have a function that counts 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 that information to be stored in the file and for that 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 above code is created the file I invoke, but save this way:

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

What am I doing wrong?

2 answers

2


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

  • The opening mode of the programme shall be "wb". of "binary writing"

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

Making 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, I could do so:

# 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 way makes the file readable to a human, but harder to load back into another program. To have both, I recommend creating a JSON file:

import json

...

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

And to carry back:

with open('novoGuarda.json', 'r') as arq:
    data = json.load(arq)

-1

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

arq = open('novoGuarda.txt', 'w').encode(encoding='UTF-8')

Browser other questions tagged

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