how to save data to disk ?

This is the place for queries that don't fit in any of the other categories.

how to save data to disk ?

Postby spamynator_1 » Fri Aug 02, 2013 1:36 am

Does anyone have suggested methods or even modules for creating a "save game" file. just needs to hold numbers.
Last edited by Yoriz on Fri Aug 02, 2013 5:48 am, edited 2 times in total.
Reason: changed title
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: save game...

Postby micseydel » Fri Aug 02, 2013 1:59 am

I'd recommend using JSON. You basically save everything in a structure made of lists or dictionaries, then call json.dumps() on that object, put the string that that function returns in a save file, and then when you need it back call json.loads().

Also, I've mostly just used the string versions of the JSON module but there are probably some that save you a step by working with files more directly.

And some people might suggest a database. That's probably overkill right now. JSON is a nice format for text files.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: how to save data to disk ?

Postby ochichinyezaboombwa » Fri Aug 02, 2013 6:12 am

yes json.dump() / json.load() might save an extra step of converting from/to string, but it's not a big deal. json is nice, simple, efficient, widespread.
ochichinyezaboombwa
 
Posts: 200
Joined: Tue Jun 04, 2013 7:53 pm

Re: how to save data to disk ?

Postby spamynator_1 » Fri Aug 02, 2013 2:27 pm

Anybody know a reccomended tutorial for someone who has never heard of that?
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: how to save data to disk ?

Postby micseydel » Fri Aug 02, 2013 4:35 pm

Heard of what? If you know how to do files in Python, you literally just use two json functions in conjunction with it. Google is your friend.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: how to save data to disk ?

Postby spamynator_1 » Fri Aug 02, 2013 4:52 pm

i got
Code: Select all
import json

location = (0,0)
mapid = (1)
inv = (0,1,2,3,4,5,6,7,8,9,10)

json.dump(os.path.join('saves', 'save1'))


how would i dump the variables location, mapid, and inv into a specific directory such as 'saves' as file 'save1'?
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: how to save data to disk ?

Postby micseydel » Fri Aug 02, 2013 6:19 pm

Code: Select all
data = {
    location: (0,0),
    mapid: (1),
    inv: (0,1,2,3,4,5,6,7,8,9,10)
}

And then dumps() that. You probably want to store your save path in there too, and note two things: (1) JSON doesn't know about tuples, so it'll silently convert them to lists, and (2) your mapid variable there isn't a tuple anyway, it's just an int, so you'll need to put a comma following the number inside the parenthesis if you want it to be a tuple/list.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: how to save data to disk ?

Postby spamynator_1 » Fri Aug 02, 2013 6:34 pm

Code: Select all
import json
location = (0,0)
mapid = (0,0)
inv = (1,2,3,4,5,6,7,8,9,10)
data = {location: (0,0), mapid: (1,1), inv: (0,1,2,3,4,5,6,7,8,9,10)}

json.dump('saves', 'save1')


this gives the error:

"Traceback (most recent call last):
File "D:\G-mail\Google Drive\Game\Notes\savegame.py", line 7, in <module>
json.dump('saves', 'save1')
File "D:\Python\lib\json\__init__.py", line 190, in dump
fp.write(chunk)
AttributeError: 'str' object has no attribute 'write'"

is that my fault? it looks like the error is in 'json'
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: how to save data to disk ?

Postby stranac » Fri Aug 02, 2013 6:48 pm

Read the docs.
Does json.dump() take two strings as arguments?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: how to save data to disk ?

Postby spamynator_1 » Fri Aug 02, 2013 6:53 pm

i didnt know what json was until this morning, useful but incomplete answers are all ive gotten so far.
unfortunately google -ing it tells me how to do everything i could ever want to do using it, except for this.
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: how to save data to disk ?

Postby stranac » Fri Aug 02, 2013 7:12 pm

Honestly, you're giving people very little reason to help you.
Have you read the docs as I've told you?
What arguments does json.dump() take?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: how to save data to disk ?

Postby spamynator_1 » Fri Aug 02, 2013 8:00 pm

"obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding="utf-8", default=None, sort_keys=False, **kw"?????

i didnt see the link before, however this is what i think the json.dumps takes??
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: how to save data to disk ?

Postby micseydel » Fri Aug 02, 2013 8:07 pm

dumps() just takes an object and creates a JSON string from it. You can write that string to a file.

Alternatively, I was suggesting you use dump() (note the lack of s) which takes a file object (not a file name, but an object you get back from open() or file()) and then writes your object to it as a string. It's less code, but now that I think about it, it might not make as much sense to a bigger. If you can't figure out dump(), though I recommend it if you can figure it out, just write the string you get back from dumps() to a file.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: how to save data to disk ?

Postby spamynator_1 » Fri Aug 02, 2013 8:25 pm

Could you please post an example?
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am

Re: how to save data to disk ?

Postby stranac » Fri Aug 02, 2013 8:37 pm

Code: Select all
import json

with open('example.json', 'w') as f:
    json.dump({'a': 1}, f)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: how to save data to disk ?

Postby micseydel » Fri Aug 02, 2013 8:44 pm

stranac's way is the pro way, but here's a simpler way you might be more familiar with
Code: Select all
import json

f = open('example.json', 'w')
json.dump({'a': 1}, f)
f.close()

and then to get back the thing you dumped,
Code: Select all
f = open('example.json') # implicit 'r' second argument
my_dict = json.load(f)
f.close()


And the longer way for the second version is
Code: Select all
f = open('example.json', 'w')
f.write(json.dumps({'a': 1}))
f.close()

and
Code: Select all
f = open('example.json')
my_dict = json.loads(f.read())
f.close()

(untested, but should be very, very close if it's wrong)
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: how to save data to disk ?

Postby spamynator_1 » Sat Aug 03, 2013 4:19 am

Thank you that works
spamynator_1
 
Posts: 63
Joined: Sun Mar 03, 2013 12:45 am


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests