1

Does anyone know a way to write a complete class instance directly into a file and not using read() to write the object as a string line wise.I used pickle.dump() to do this but the text file it is creating is not in a readable form.If any other way please tell.And after that to read all of those objects in the file object wise.And the print it .

Dipendra bhatt
  • 729
  • 5
  • 14

2 Answers2

0

The problem with readable formats is that, python needs to know whats a readable format. What I mean by that is that needs to know how to serialize your object. You can try json which will automatically convert to text and back common POD data. Even nested, so you can have a complex dictionary with nested list and nested dict, that will be serialized fine as long as you have pod data in it. This means that you might want a serialize/deserialize method on you class that spit out a block of pod data and re-inizilize the class with the same block of data.

I worked on a library to achieve that, you might be interested in having a look, it leverages python descriptors. https://github.com/giordi91/storable_class

Marco Giordano
  • 600
  • 4
  • 14
  • Ok the file should be in human readable format is not manadatory. – Dipendra bhatt Feb 18 '16 at 14:41
  • Ok after putting many instance in the file how do i read them. – Dipendra bhatt Feb 18 '16 at 14:42
  • I mean how do i read the data in file and then store the data in an instance and then print it – Dipendra bhatt Feb 18 '16 at 14:43
  • with json, and i think with pickle asewll you get out what you put in. ( I did not use pickle in ages so don't quote me on that). In json if you have a list of dict, where each dict, represent a class, you will get the same list with same number of dict, in same order. Check the save and load method here https://github.com/giordi91/storable_class/blob/master/st_json_utils.py – Marco Giordano Feb 18 '16 at 14:44
  • sorry bro but i am not that much familiar with jason . – Dipendra bhatt Feb 18 '16 at 14:47
  • if you check the save and load function , you will see is a piece of cake:) it is like 5 lines of code for each function. I cannot paste the code in the comment but is super straight forward:) have it a look. – Marco Giordano Feb 18 '16 at 14:50
  • Ok i will take a look – Dipendra bhatt Feb 18 '16 at 14:54
0

pickle was mentioned to make Python-readable text outputs. If you want to print it in your own format, defining your own method obj.ouput() will not be that hard: You can loop through the attributes and methods of the object with dir(object) and write them on separate rows of a text file.

def output(self):
    with open(self.__name__ + "output.txt","w") as f:
        f.write(self.att.__name__ +" "+ self.att + "/n") for att in dir(obj) if not att.startswith('__')

If you don't want the methods as well, you can check that callable(obj.att) is false when looping.

See further details: Iterate over object attributes in python

Community
  • 1
  • 1
Mathieu B
  • 451
  • 3
  • 12