-2

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.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
  • I personally would store this in a csv, with a format as follows. – chess_lover_6 Sep 15 '21 at 15:56
  • Does this help you? [I tried to make a balance command in discord.py but it didn't worked and gives unexpected errors](https://stackoverflow.com/questions/66546883/i-tried-to-make-a-balance-command-in-discord-py-but-it-didnt-worked-and-gives-u) – Dominik Sep 15 '21 at 16:43
  • Does this answer your question? [Writing Unicode text to a text file?](https://stackoverflow.com/questions/6048085/writing-unicode-text-to-a-text-file) – bad_coder Sep 15 '21 at 21:59
  • Kindly refrain from beginning your questions with "okay", "so", and other redundant & irrelevant stuff. – desertnaut Sep 15 '21 at 22:05

2 Answers2

1

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!

Danvin
  • 61
  • 4
-2

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()
Ehsan Rahi
  • 61
  • 7