M
M
Maxim Siomin2020-06-05 18:04:49
Python
Maxim Siomin, 2020-06-05 18:04:49

How to save data in python?

There is a certain variable, for example user_data = input()
It is necessary to save it somewhere, so that when you open the program again, it already exists. I do like this:

with open('file.txt', 'w') as file:
    file.write(user_data)

And then I get data like this
user_data_list = []

with open('file.txt', 'r') as file:
    for line in file:
        user_data_list.append(line)

user_data = user_data_list[x]

But I think real programmers do things differently.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
soremix, 2020-06-05
@MaxSiominDev

Apparently, you have a list (or dictionary?) With users.

  1. Why are you getting the element through the index of the list? Why not make a dictionary, where the key will be the user ID, and the value will be the parameters already needed in the dictionary
  2. Better use json library and its json.dump() and json.load() functions to save data to file and load from file respectively

I
Ivan Solomennikov, 2020-06-05
@ivsol

pickle -
json text - key : value
struct(name, age, mail, etc) - db: sqlite3, which doesn't need to be installed separately, mysql, postgresql, etc

D
Deleting Account, 2020-06-05
@Andriy_Kosmenyuk

But I think real programmers do things differently.

Real programmers read the python tutorial first , and then the article. python library https://docs.python.org/3/library/index.html. Here's about ways to store data in Art. python library https://docs.python.org/3/library/persistence.html. You can also use json .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question