Python- How to better store input (date) entries?

Asked

Viewed 758 times

0

I have used write ("with open" "write") to overwrite and/or add date(personal names, address,phone) in the.py scripts, and it has worked out, but if I don’t take care it gets messy, both because the scripts that store date get huge, and for having to care-los,creating a network of communicable scripts that requires care when importing. This is boring and probably unnecessary. Libraries like Json would be a better alternative ? If yes, what is the most suitable for this purpose ( registration information) ? There seem to be many ...

1 answer

2


Option Database

Maybe the best thing for you is to use a database. It can be the same Sqlite, which already comes with Python. Now database is not a magic formula.

It is cool because it allows you to read or write each record individually on disk, knowing that when you do an update operation the data is already saved and safe. On the other hand, it requires you to understand a whole language and different concepts - SQL - even if you won’t use direct SQL, there are Python libraries that are Object-Relational adapters, like Sqlalchemy - you will have to understand of object orientation and use of classes and instances, some details of Sqlalchemy, and it generates the SQL commands for you (so you can only "understand" SQL and not necessarily "learn"). But it’s still enough for a simple program;

If you use sqlite, which comes with Python, you do not need to configure or install an external database, such as Mariadb or Postgresql

JSON files option

If the number of entries is small - for example, less than 2000, you can keep the data in memory while the program is running, and each time the program finishes, or every x minutes, save all the data - and you can use Json for this yes.

The advantage is that your data is already in a format that is directly usable by other technologies, or editable directly in a text editor. What you have to learn to use is very simple.

The biggest drawback might be that JSON has a few primitive data types, and if your records have objects like dates, timestamps, images, sets, you’ll have to treat this data in a special way.

Arquivos Pickle

Python has a package called "pickle" in the standard library - it can transform almost any Python object into a sequence of bytes, which can be stored in a file, and retrieve the objects from there. Your program looks very much like the approach required for JSON: it’s easier to read and write all the records at once, and work with the data in memory, etc... the biggest difference is that the resulting file cannot be read directly in a text editor or by technologies other than Python. (And, of course, the fact that reading and writing everything at once imposes a practical limit of a few thousand records).

If you choose to do so, just keep all your data in a large list or dictionary and use functions like these:

import pickle

dados = {}

...

def salvar():
with open("dados.bin", "wb"):
    pickle.dump(arquivo, dados)

def carregar():
   global dados
   dados = pickle.load(open("dados.bin"))

Browser other questions tagged

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