N
N
NekIT2021-08-13 13:40:30
Python
NekIT, 2021-08-13 13:40:30

How to save data to .txt in Python?

I mean automatic saving of data, not through SaveAs.
That is, to save data from variables.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Kuts, 2021-08-13
@NekIT_049

Pickle module as an option, if I understand correctly:
Recording:

import pickle

# An arbitrary collection of objects supported by pickle.
data = {
    'a': [1, 2.0, 3, 4+6j],
    'b': ("character string", b"byte string"),
    'c': {None, True, False}
}

with open('data.txt', 'wb') as f:
    # Pickle the 'data' dictionary using the highest protocol available.
    pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)

Reading
import pickle

with open('data.txt', 'rb') as f:
    # The protocol version used is detected automatically, so we do not
    # have to specify it.
    data = pickle.load(f)
print(data)
# {'a': [1, 2.0, 3, (4+6j)], 'b': ('character string', b'byte string'), 'c': {None, True, False}}

G
GavriKos, 2021-08-13
@GavriKos

https://www.w3schools.com/python/python_file_write.asp
Google banned?

H
Hemul GM, 2021-08-13
@HemulGM

Save in JSON. Use serializers/deserializers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question