Answer the question
In order to leave comments, you need to log in
The snake does not want to extract the dictionary from the *.txt file, how can I help it?
There is a db.txt file that contains only one line^
{'000000': {'name': 'patient_zero', 'bank': 500}, '111111': {'name': 'Badmajor', 'bank': 1000}, '222222': {'name': 'Badmajor1', 'bank': 1000}}
with open('db.txt', 'r', encoding='utf-8') as db:
db_dict = db.read()
user_data = db_dict['000000']
name_user = user_data['name']
print(name_user)
Traceback (most recent call last):
File "\main.py", line 5, in
user_data = db_dict['000000']
TypeError: string indices must be integers
Answer the question
In order to leave comments, you need to log in
theoretically can pull out a dictionary
import json
with open('db.txt', 'r', encoding='utf-8') as db:
db_dict = json.load(db)
You need to save the dictionary as .data
For example:
import pickle
# имя файла, в котором мы сохраним объект
shoplistfile = 'shoplist.data'
shoplist = ['яблоки', 'манго', 'морковь']
# Запись в файл
f = open(shoplistfile, 'wb')
pickle.dump(shoplist, f) # помещаем объект в файл
f.close()
del shoplist # уничтожаем переменную shoplist
# Считываем из хранилища
f = open(shoplistfile, 'rb')
storedlist = pickle.load(f) # загружаем объект из файла
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question