python - How to put pickle dict value into an object -
i using pickle dump dictionary object txt file. need grab dictionary file , extract values , put them inside of object string.
my dictionary looks this:
obj_dict = { 'name': 'myid', 'value': 'usdf23444', 'name': 'myid2', 'value' : 'asdfh3479' }
part of dilemma have there 2 'name'
, 2 'value'
in dictionary , need grab each separately.
here code using:
import pickle filepath = file.txt output = open(filepath, 'rb') obj_dict = pickle.load(output) in obj_dict: newstring = "value1=" + i['value1'] + "value2=" + i['value2'] print(newstring)
i know code doesn't work, i'm more showing need end result need each value put string can use later. how can reference 'value1'
, 'value2
' correctly? getting error when trying 1 'value': typeerror: list indices must integers or slices, not str
edit comment 2
i'm not sure if that's true, can run code:
output = open(filepath, 'rb') obj_dict = pickle.load(output) in obj_dict: print(i['value'])
and output is:
usdf23444 asdfh3479
after update, looks list of dicts. try:
strings = ["value{}={}".format(i, d['value']) i, d in enumerate(obj_dict)]
Comments
Post a Comment