0

I have a serious snag in my programming, and it might be an easy fix, but anytime I find something that looks like the right forum post, it's not quite right.

I'm using a file to store data created from one part of my program, like a high score, number of times played, and player name.

I store all this data just fine in the file with a tuple set of ("string key", key's value). I like the ease of retrieval with the dict() command, and have worked with both, but I have to convert the entire data set into a string in order to store it to the file.

And

When I retrieve the data for the next time I open the program, I can't seem to add to the "number of times played" or change the players name for the high score..

It looks a little like this:

import sys
import random
import pickle
save=open ("(path to file)/newgame.txt", 'r+')
save.seek(0)
hiscore=(save.readlines())
def Savedata():
    global save
    global name
    global highscore
    global numplays
#this is where the ideal code would split, modify, and repack the data from the file    
    save.seek(0)
    save.write(str (hiscore))
print (save)
save.seek(0)
save.close()

Can anyone give me any direction add to what I should look into.. I'm on my phone and can't give proper code examples right now...

Ryan Harger
  • 47
  • 1
  • 9
  • You may want to clarify you problem. I'm having trouble understanding what you're trying to do here. – ljk321 May 07 '15 at 02:28
  • I needed to convert a set of tuples of ("string", integer) into a string, and back into a set of tuples – Ryan Harger May 07 '15 at 05:48

1 Answers1

0

Have you considered using an actual serializing library? For example json is very useful. Particularly if you're just dumping a dict.

For example:

>>> d = {'level':2, 'position':[2,3]}
>>> import json
>>> json.dumps(d) # or json.dump(d, save) in your code
'{"position": [2, 3], "level": 2}'

That way you could simply load the string using json.loads. Or directly a file using json.load.

spalac24
  • 1,076
  • 1
  • 7
  • 16
  • Thanks, I really am beginning in this endeavor, I was using what I knew, and I don't know json at all, I will try and learn more about it, maybe that will be the best solution. At the time, I knew about tuples and dicts and lists and saving and recalling strings. I'm always willing to add more to my knowledge base for programming :-) – Ryan Harger May 07 '15 at 05:50
  • Revision: I haven't fixed it yet, but i noticed that the run of the code i was trying also alerted me to an error when i tried to split the file back into tuples. '_io.TextIOWrapper' object has no attribute 'split' ... which i was ignoring. There was a post that helped a lot [here, posted by safeer](http://stackoverflow.com/questions/17569679/python-attributeerror-io-textiowrapper-object-has-no-attribute-split) if anyone in the community has this problem and can't solve it like i couldn't – Ryan Harger May 07 '15 at 17:05
  • What have you tried so far? Did you follow my example of using json? Post maybe a minimal working exmaple so we can reproduce your error. – spalac24 May 07 '15 at 21:59
  • I have too much to post, this will have to be a second question, the short version is that you have successfully answered my original problem – Ryan Harger May 11 '15 at 00:59