hi, every time I restart the program I loose all the variables related to a user (i.e. user_data[user_id] { var 1, var 2,…} ) that have been created and updated along the telegram bot experience the first time the user connect to the bot. I want to save this variables on a file and load them when I restart the program (after an update or a crash for example) when the user_id reconnects to the bot. Is it possible?
You could use pickle
1 Like
Below are examples of pickling, saving, and retrieving data. You can of course modify the data after retrieval, then pickle and save it again.
Pickling and saving:
import pickle
oaks = {'White Oak' : 'Quercus alba',
'Black Oak': 'Quercus velutina',
'Gambel Oak': 'Quercus gambelii'}
# pickle and save in a file
with open('oak_data_file.pkl', 'wb') as outf:
pickle.dump(oaks, outf)
print("pickled and saved!")
Retrieving the pickled data:
import pickle
# load the pickled data
with open('oak_data_file.pkl', 'rb') as inf:
in_data = pickle.load(inf)
print(in_data)
Output:
{'White Oak': 'Quercus alba', 'Black Oak': 'Quercus velutina', 'Gambel Oak': 'Quercus gambelii'}
2 Likes