D
D
Danil Neskazhu2020-08-10 14:42:27
Python
Danil Neskazhu, 2020-08-10 14:42:27

Simple DB/Storage without SQL in python?

Hello.

Faced such a moment that you need to create a simple database so that the necessary data is saved after the program is restarted.

The data in the code is written in this way.

user_data = {"ID" :  {"Товар" : "Цена", "Товар1" : "Цена1", ...}}


That is, a two-dimensional dictionary, where at the first level the key is the user ID, and the value is another dictionary with keys - goods and values ​​- prices.

How to save and store it? What libraries to use for this?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
SKEPTIC, 2020-08-10
@dannight0151

Encode to json and write to file
Save

import json

user_data = {}
f  = open("config.txt", "w")
f.write(json.dumps(user_data))
f.close()

Read
import json

f = open("config.txt", "r")
user_data = json.loads(f.read())
f.close()

S
Stanislav Pugachev, 2020-08-10
@Stqs

https://pypi.org/project/tinydb/

R
Roman Mirilaczvili, 2020-08-10
@2ord

Judging by the data structure, SQL DBMS is better suited. For starters, you can use SQLite, or you can immediately use MySQL.

R
Roman Kitaev, 2020-08-10
@deliro

Shelve

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question