C
C
CatRoys2020-06-04 19:33:22
Python
CatRoys, 2020-06-04 19:33:22

How to store a dictionary (nicknames) in Python?

Hello, I wanted to make a nickname system so that each user has their own nickname. I understood how to implement the system, but there was a problem with saving these same nicknames, for this you need a file, here I was already thinking and did not understand how to implement it, I googled, made my options all useless. Please help.
Script to change nickname:

if msg.startswith('%ник'):
      nick = msg.lstrip('%ник').strip()
      user_id = event.obj["from_id"]
      if len(nick) < 3:
        vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "⚠ | Никнейм слишком короткий.", "random_id": 0})
      if len(nick) > 15:
        vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "⚠ | Никнейм слишком длинный.", "random_id": 0})
      else:
        users_nick[user_id] = nick
        print(users_nick)
        #Функция записи в .txt файл.
        vk.method("messages.send", {"peer_id": event.object.peer_id, "message": "✔ | Никнейм успешно изменён.", "random_id": 0})

At the beginning of the code, there should be a function to write the lines of the file to a dictionary.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Deleting Account, 2020-06-04
@CatRoys

Have you tried using json . If it doesn't suit you, then read about other ways to store data in python https://docs.python.org/3/library/persistence.html

E
ediboba, 2020-06-05
@ediboba

sqlite, mongodb, redis, memcached... and indeed any subd. Connectors and libs have been written for everything for a long time - and it will be easier and better than writing a bicycle

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question