-1

I am trying to make a relatively simple game that saves all of it's data to .txt files. The text file I have to store account data uses a simple format such as follows:

Username
PIN
Balance
--
Username
PIN
Balance
--

Etc. Etc.

What I want to do is just change the balance value. At the moment, the only way I see to do this is to add each line to a list, change the balance value, then rewrite all of the lines one by one back to the text file.

Is there an easier way to do this? If so, how?

Someone
  • 55
  • 1
  • 7
  • what have you tried till now ? – Hari Krishnan Aug 23 '18 at 06:07
  • Why don't you store it on a csv. in the future it will let you perform more difficult operations. – jalazbe Aug 23 '18 at 06:08
  • The easier way is to use a database. Like sqlite. It will save you a lot of troubles in the future. – warvariuc Aug 23 '18 at 06:09
  • I have not tried anything yet, just wondering if there is any easy way to do this. I am also relatively new to python. – Someone Aug 23 '18 at 06:10
  • Using a database may be not easier in this particular case, but if you are making a game you will need to retrieve data, filter, sort, group by -- the database does all of this for you in a standard and convenient way. As a bonus you get data validation, transactions. https://www.google.com/search?q=python+sqlite+tutorial – warvariuc Aug 23 '18 at 06:13
  • You should rethink your data structure. If you absolutely need it this way, you can check [in place editing](https://stackoverflow.com/a/20593644/7570485). But I wouldn't recommend it. – Xnkr Aug 23 '18 at 06:13
  • You will have to use a db or such a no-sql tool etc.. Begin to learn some basics about db, it is very easy with python. https://pythonspot.com/python-database-programming-sqlite-tutorial/ – Ferhat S. R. Aug 23 '18 at 06:17
  • Ok, will look into it. – Someone Aug 23 '18 at 06:23

1 Answers1

0

That obviously is a very simple file structure, thus it has it's limitations. You will probably won't notice any performance problems until you have more than tens of thousands of lines. As said in the comments you're gonna need a better tool after that.

If you keep using this method, you don't really need to load everything, and save it back on the disk after updating the values.

You can stream reading the file and write back to a temp file at the same time. Once you come across to the group you need to update, you need to update the Balance value of course. After you're done with updating you can delete the old file and rename the temp file with your actual file.

with open('input.txt') as original, open('output.txt', 'w') as target:
    while True:
        label1 = original.readline()
        pin = original.readline()
        label2 = original.readline()
        balance = original.readline()
        if pin.strip('\n') == 'PIN':
            balance = 'new-balance\n'
        target.writelines([label1, pin, label2, balance])
        if not balance:
            break
# Delete original and rename new file.
dereli
  • 1,814
  • 15
  • 23