1

I am working with a dictionary. From different resources, I understood that is comfortable to use pickle. I want to use pickle to save triples like: subject: predicate: object, but later I want to load them and fill an array Assuming, my code:

import pickle

file_name = 'fna.txt'

class DictClass:
    def __init__(self, subj, predicate, obj ):
        self.subj = subj
        self.predicate = predicate
        self.obj = obj
    def __repr__(self):
        return 'DictClass(%r, %r, %r)' % (self.subj, self.predicate, self.obj)
    def __eq__(self, other):
        return (self.subj == other.subj) and (self.predicate == other.predicate) and (self.obj == other.obj)

data = {'triple': DictClass(17,'subClassOf', 34), 'triple': DictClass(22,'subClassOf', 44)}

print('data:', data)
print('repr(data):', repr(data))
print("data == eval(repr(data), {'DictClass':DictClass})?:", data == eval(repr(data), {'DictClass':DictClass}))

# Load the dictionary back from the pickle file.  
def ld_dict(fname):
    reader = pickle.load(open(fname, 'rb'))
    print(reader)
    return True

# Save a dictionary into a pickle file.
def sv_dict(fname):
    pickle.dump( data, open( fname, "wb" ) )
    return True

if(sv_dict(file_name)):
    print('File saved!')

if(ld_dict(file_name)):
    print('File loaded!')

In generally, I want to save data in the dictionary and later load it and make some processing about it but I have a problem. I cannot access to the information into the loaded data. In function ld_dict I was trying to read information and have results:

data: {'triple': DictClass(22, 'subClassOf', 44)}
repr(data): {'triple': DictClass(22, 'subClassOf', 44)}
data == eval(repr(data), {'DictClass':DictClass})?: True
File saved!
triple
File loaded!

How I can get information in the way like, for example, JSON. How to parse JSON I found here(How to parse json data in Python?). Also, I have seen this resource: how to save a dictionary in pickle Thank you for help

Vasyl Lyashkevych
  • 1,920
  • 2
  • 23
  • 38

1 Answers1

2

Comment: ... but I cannot see : 'triple': DictClass(17, 'subClassOf', 34)

There is no 'triple': DictClass(17, 'subClassOf', 34) in your example data, because your example dict data is invalid!
Keys in a dict have to be unique, you overwrite the first 'triple' with the second 'triple'. Maybe you want a list instead of a dict.


Question: I cannot access to the information into the loaded data.

You get the same data object back, assing it to a variable to become a instance.

Note: I use in Memory dumps/loads, replace with dump/load to write/read to/from file.

This is working for me, for instance:

def print_data(d):
    print('data:', d)
    print('repr(data):', repr(d))
    print("data == eval(repr(data), {'DictClass':DictClass})?:", data == eval(repr(d), {'DictClass':DictClass}))
    print('\n')

data = {'triple': DictClass(17, 'subClassOf', 34), 'triple': DictClass(22, 'subClassOf', 44)}
print_data(data)

#pickle.dump( data, open( fname, "wb" ) )
data_pickeld = pickle.dumps( data )

#data_unpickeld = pickle.load(open(fname, 'rb'))
data_unpickeld = pickle.loads( data_pickeld)

print_data(data_unpickeld)

Output:
data: {'triple': DictClass(22, 'subClassOf', 44)}
repr(data): {'triple': DictClass(22, 'subClassOf', 44)}
data == eval(repr(data), {'DictClass':DictClass})?: True

data: {'triple': DictClass(22, 'subClassOf', 44)}
repr(data): {'triple': DictClass(22, 'subClassOf', 44)}
data == eval(repr(data), {'DictClass':DictClass})?: True

Tested with Python: 3.4.2

stovfl
  • 14,998
  • 7
  • 24
  • 51