T
T
tyfh502021-09-18 15:44:56
Python
tyfh50, 2021-09-18 15:44:56

Using append for typography?

I have functions to save and read "database" (texter)

def save_bd(users, filename):
lines = []
for user in users:
lines.append(f'"id" : {user.id}, "mode " : "{user.mode}", "admin" : {user.admin}, "ban" : {user.ban}')
lines = '\n'.join(lines)
with open(f'{tdir( __file__)}/{filename}.txt'.replace('\\', '/').replace('vova.py/', ''), "w", encoding = 'utf-8') as file :
file.write(lines)
file.close()

def read_bd(filename):
users = []
with open(f'{tdir(__file__)}/{filename}.txt'.replace('\\', '/ ').replace('vova.py/', ''),"r", encoding = 'utf-8') as file:
lines = [x.replace('\n', '') for x in file.readlines()]
file.close()
for line in lines:
line = eval('{' + line + '}')
if line != '{}':
users.append(User(id = line['id'], mode = line["mode" ], admin = line['admin'], ban = line['ban']))
return users
This is for the VK bot. To give the same admin in a conversation, you need to change the "admin" parameter to another person to 1. I use append for this, but when I save (save_bd()), a separate line is created in the text editor. It is necessary that the first one be changed, and not a new one be created. How can this be resolved?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question