Okay, so I have a tuple for a game, and this is it: ['Hondros', {'Equiped': [['none', [], 0], ['Rags', [0, 1, 0, 0], 1, 1], ['Mocassins', [0, 0, 1, 0], 1, 1], ['Gauntlets', [0, 0, 0, 1], 6, 5]], 'Equipment': [0, 1, 1, 1], 'Stats': {'AC': 14, 'Strength': 15, 'Constitution': 14, 'Level': 1, 'HP': 12, 'Experience': 0, 'Dexterity': 12, 'Intelligence': 12, 'Base AC': 8, 'Wisdom': 11, 'HP max': 12}, 'Inventory': [], 'Gold': 0}] Anything inside it can change, including number of tuples inside the dictionaries. Anyways, this is how my program stores the character's information. There might be more added on. Now, this is what I want to do. I want to open a file, convert the tuple to a long integer, through use of the ord(). I've already done this, and saved it as character.sav, where character is the character's name. Now, when I open it, and convert it back, I now have a string of the above tuple. How do I go about reconverting it? Doing a tuple of it just makes it weird...

EDIT: Er... I seem to have my definitions of tuple and list confused... these are lists, not tuples. Oh well... same difference really

Edited 6 Years Ago by hondros: n/a

You should use modules like pickle or marshal to serialize and load your datas

import pickle
c=['Hondros', {'Equiped': [['none', [], 0], ['Rags', [0, 1, 0, 0], 1, 1], ['Mocassins', [0, 0, 1, 0], 1, 1], ['Gauntlets', [0, 0, 0, 1], 6, 5]], 'Equipment': [0, 1, 1, 1], 'Stats': {'AC': 14, 'Strength': 15, 'Constitution': 14, 'Level': 1, 'HP': 12, 'Experience': 0, 'Dexterity': 12, 'Intelligence': 12, 'Base AC': 8, 'Wisdom': 11, 'HP max': 12}, 'Inventory': [], 'Gold': 0}]
f=open('test.pkl','wb')
pickle.dump(c, f)
f.close()
f=open('test.pkl','rb')
d=pickle.load(f)
f.close()
print d[1]['Stats']

Eventually, you can use exec but this is not to be recomended

a = "['Hondros', {'Equiped': [['none', [], 0], ['Rags', [0, 1, 0, 0], 1, 1], ['Mocassins', [0, 0, 1, 0], 1, 1], ['Gauntlets', [0, 0, 0, 1], 6, 5]], 'Equipment': [0, 1, 1, 1], 'Stats': {'AC': 14, 'Strength': 15, 'Constitution': 14, 'Level': 1, 'HP': 12, 'Experience': 0, 'Dexterity': 12, 'Intelligence': 12, 'Base AC': 8, 'Wisdom': 11, 'HP max': 12}, 'Inventory': [], 'Gold': 0}]"
exec("b=" + a)
print b[1]['Stats']

You should use modules like pickle or marshal to serialize and load your datas...

I really agree with jice, but keep in mind the Pickle module shouldn't be used for anything that would requires good security, theres a good amount of exploitable loop holes that will most likely never be fixed, and are extremely easy to exploit.

Okay, that's fine, for now I guess. But see, that'll allow for easy hacking of the character's when it is saved. What I am doing, is converting each of the ascii characters in the file to their corresponding number, and adding a shift, then saving it. Thus, I have to convert the list to an integer, but I can't convert it back... When I convert the numbers, I get a string, but I need a list. How do I convert the list?
But, I do thank you for showing me the pickle module, that works better than the standard open/write functions :D

So you literally take your object and perform a str(object) ? And that's what you're trying to turn back into a proper python object? For that you would use a simple eval():

to = ['This is my list', 'With Stuff', [0,1,2,3,4], 'Foo', 112, 5, '57 + 1']
my_saved_ver = str(to)
tr = eval(my_saved_ver)
print type(tr)             # Prints <type 'list'>
print type(my_saved_ver)  # Prints <type 'str'>

HTH

Yeah, I had just figured it out xD I was chatting with someone on Gmail, when I did a search, found eval(), then went back to my mail, and saw this xD Wierd, eh? But thanks, :D
Here's the data:

Hondros = ['Hondros', {'Equiped': [['Death Mask', [1, 0, 0, 0], 8, 6], ['Rags', [0, 1, 0, 0], 1, 1], ['Mocassins', [0, 0, 1, 0], 1, 1], ['Gauntlets', [0, 0, 0, 1], 6, 5]], 'Equipment': [1, 1, 1, 1], 'Stats': {'AC': 24, 'Strength': 15, 'Constitution': 14, 'Level': 1, 'HP': 12, 'Experience': 0, 'Dexterity': 12, 'Intelligence': 11, 'Base AC': 8, 'Wisdom': 12, 'HP max': 12}, 'Inventory': [['Death Mask', [1, 0, 0, 0], 8, 6], ['Rags', [0, 1, 0, 0], 1, 1], ['Mocassins', [0, 0, 1, 0], 1, 1], ['Gauntlets', [0, 0, 0, 1], 6, 5]], 'Gold': 0}]

Then, for saving, I do this: (I'm going to be adding a caesar shift later)

save = ""
for x in range(0, len(str(Hondros))):
		y = str(ord(str(Hondros)[x]))
		if len(y) < 3:
			y = "0"+y
		save = save + y
savefile = character[0] + ".sav"
f = open(savefile, "wb")
f.write(save)
f.close()

That gives a rather long list of numbers :D
And for loading the file's contents, I do this:

load = ""
for x in range(0, len(save), 3):
	load = load + chr(int(save[x:x+3]))
character = eval(load)

:D So yeah, if anyone wants to use my code as a template, go for it

Edited 6 Years Ago by hondros: Had to fix the saving part

This question has already been answered. Start a new discussion instead.