2018-09-27 23:41:30

hi all, i am having trouble with a python dictionary.
i wrote a dictionary to a file, and it was successfull.
my problem now is that i want to asign one of the values in the dictionary to a variable, but i can't because of the dictionary being wrote to a file, it stays a string. is there a way i can convert the same string to a dictionary?

best regards
never give up on what ever you are doing.

2018-09-28 01:46:34

Pickle. No, not that kind of Pickle, I mean the Pickle Library. What you want to do is pickle the data before you write it to an external file, that way when you reload the data it recreates it as a data object instead of a string. Example:

import pickle

#open file
fil = open('box.txt','w')

#create dictionary
box = {'thing':1,'bing':2}
print(box)

#dump data from dictionary into loaded file
pickle.dump(box,fil)

#close the file
fil.close()


#re-open file
bil = open('box.txt','r')

#un-pickle data from the file
box = pickle.load(bil)
print(box)

#close the file
bil.close()
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up

2018-09-28 08:13:02

thanks a lot magurp244, sorry for asking it here, i just could not find an answer to my question on google. i would try it now

best regards
never give up on what ever you are doing.

2018-09-28 08:19:11

now when trying to dump the contents to a file i get
TypeError: write() argument must be str, not bytes

best regards
never give up on what ever you are doing.

2018-09-28 08:25:35

Try writing the file in binary mode:

fil = open('box.txt','wb')

If it gives you an error on reading it, do the same:

bil = open('box.txt','rb')
-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up +1

2018-09-28 08:30:21

thanks magurp244, that seemed to fix the problem. now if i want one of the keys to be a variable, should i just say position=box["pos"]?

best regards
never give up on what ever you are doing.

2018-09-28 08:31:32 (edited by pauliyobo 2018-09-28 08:32:30)

it depends on whether you want the dictionary to be loaded and dumped entirely, or if you  want to retrieve only the values.
I'll use json in this case.
If you want to dump a dictionary entyrely:
import json
dict1 = {'value1': 1, 'value2' : 2, 'value3': 3}
f = open('result.txt')
f.write(json.dumps(dict1))
f.close()
loading:
import json

f = open('result.txt', 'r')
dict1 = json.loads(f.read())
f.close()
If instead, you have predefined values that you want to load from a file:
dict1 = {'value' : 1}
f = open('result.txt', 'w')
f.write(str(dict1['value'))

If you want to get in touch with me you can follow me on Twitter
have a nice day.
Paul

2018-09-28 08:37:40

Technically its already a variable, just inside a dictionary, but yes that works fine.

-BrushTone v1.3.3: Accessible Paint Tool
-AudiMesh3D v1.0.0: Accessible 3D Model Viewer

Thumbs up

2018-09-28 08:39:40

thanks paul, i already found out how to work around this.
what i do is i load the file with pickle but like this:
dict=pickle.load(f) #asuming that f is the file with the contents loaded in write binary form
then to access the value, i just say for example my_x=dict["pos"]
and i even printed it to see if it works, and it did.
i used the type(), type(my_x), and it returned an int. thanks for all the help

best regards
never give up on what ever you are doing.

2018-09-28 19:27:14

oh and thanks magurp, you also helped with this alot

best regards
never give up on what ever you are doing.