python first langauge i'm learning , i'm studying file input/output systems storing simple data. i've briefly done exercise regarding simple writing , pickling. purposes, use simple writing, edit "save files".
i have been successful in storing , retrieving data when data string. problem when store object, stores unreadable format:
<__main__.character object @ 0x0078fe10> how can turn unreadable format above in object? python have built-in function allows this?
or have convert object string, containing important variables before writing in data file?
edit: oops, forgot code!
class character(object): def __init__(self,name,exp,atk): self.name = name self.exp = exp self.lvl = int(self.exp/100) self.health = 10 self.atk = atk if atk == none: self.atk = lvl*1.5 def stats(self): print(self.name, end=" level ") print(self.lvl) print("with", self.health, "health") and here's reading part of i/o system. can see reads data > converts string list > splits arguments in list > use arguments object creation.
with open('chardata', 'r') data: data = data.read() # read contents of file memory. print (data) my_list = data.splitlines() print (my_list[0]) # print list. char_data = ast.literal_eval(my_list[0]) new_character = character(*char_data) then have own function display object stats:
new_character.stats() spits out
harley level 3 10 health so can see, works fine. if use "newfile.write(new_character)" string of object, "<main.character object @ 0x0215fe10>" above.
i've been messing str in object past hour, haven't found way use function find solution , looking guidance more experienced person.
edit matt habel:
def __str__(self): print ("[\"", end="") print (self.name, end="") print ("\"", end=", ") print (self.exp, end="") print (",", end=" ") print (self.atk, end="") print ("]") this outputs this:
["harley", 300, 3] print(new_character) typeerror: __str__ returned non-string (type nonetype) my reasoning making own "list" can use "ast" module define list arguments. later convert list above "new_character2", , write using this:
newfile = open("chardata","w") newfile.write(new_character2) now use:
instead of complicated parsing above, i'm using return function.
def export(self): self.name = str(self.name) self.exp = str(self.exp) self.atk = str(self.atk) matt_data = "[\"%s\", %s, %s]" % (self.name, self.exp, self.atk) return matt_data matt_export = new_character.export() print(matt_export) spits out:
["harley", 600, 3] and no error code!
that format default string representation python gives objects. can check calling str(object) on instance of 1 of classes. if want readable string of object, can provide custom __str__(self) method on class. change objects string representation output file.
as actual question turning default representation 1 of objects: it's impossible. that's because default representation tells name of object , memory location stored. because memory transient (it isn't available program across different times run it), can't take memory address , turn 1 of objects.
if want this, there few ways go it:
- you provide human readable
__str__method. output data can read , make easy parse object. you'll have reading object's string representation file, extracting data necessary create new object, , creating object. - you can serialization methods. output object in form easy computer read , write, you'll unable read it. there lots of serialization libraries, python provides
picklelibrary out of box. - you can human readable data formats json, yaml, etc. using 1 of these provide both human readable file , great libraries working files.
Comments
Post a Comment