I'm making a discord bot which runs an economy type system in python. I want to use a text file to store user data, but I'm not quite sure how I'd do this. What I mean is that, depending on which user does a /balance command, it would search for their specific data and return their balance.
CodePudding user response:
If your building something & requires the need to store user data such as account balance, username, date account was created, transaction history & so.
NEVER USE SIMPLE TEXT FILES FOR SUCH IMPLEMENTATIONS
You should use a Database which is ACID Complaint meaning The Database should be :
A - Atomicity : That is a particular action is completed successfully or completely fails (Incase of transfer of funds or balance you don't want the balance to be deducted from the transfer's account & not transferred to the recipient's account, and you also don't want the transfer to fail & but the credit to succeed to the recipient's account when nothing was deducted from the transfer's account)
C - Consistency : Only Valid data that follows the rules should be added to the database & the database's integrity is maintained.
I - Isolation : Ensures that each transaction is isolated & not affected by other transactions.
D - Durability : Ensures that once a transaction is committed the data persists in the system even incase of subsequent failures.
There is an Excellent Database which is ACID Compliant, it's A NoSQL Database, It's Called MongoDB.
You could use MongoDB's Python Driver to connect to a Mongo Server. https://www.mongodb.com/languages/python
Also MongoDB is flexible in how you choose to structure the data you want to store!
CodePudding user response:
I think you want to say like this You can just copy and paste in your IDE then you can implement as you want
user = input("User name : ")
password = input("Enter password : ")
text_file = open("user_data.txt", "a ")
"""create user_data.txt in working directory"""
text_file.write("user Name : " user "\t" "password : " password "\n")
text_file.close()