converting object to str and back to objects
i had a list of obj
[<DiffMatchPatch.patch_obj instance at 0x152a0b00>, <DiffMatchPatch.patch_obj instance at 0x152a0ab8>]
i later converted them to a strs - then i got
["@@ -797,44 +797,8 @@\n : %0A\n- print 'WithDrwaing %25d' %25 shs%0A\n \n", "@@ -854,158 +854,4 @@\n e %0A%0A\n-'''%0Aif __name__ == '__main__': 开发者_如何学Go%0A b=Transcations(amount=9) %0A b.deposit() %0A b.withdraw(2000) %0A b.interest(30) %0A b.chk()%0A'''%0A%0A%0A\n"]
is it possible to convert the strs back to objects?
You must use Pickle for this purpose...
from cPickle import dumps, loads
lst = [1,2,3]
pckld = dumps(lst)
print (pckld)
>> '(lp1\nI1\naI2\naI3\na.'
loads(pckld)
>> [1,2,3]
Although it is not exactly what you ask for, maybe the "serialization" can solve your problems:
http://en.wikipedia.org/wiki/Serialization
http://docs.python.org/library/marshal.html
http://docs.python.org/library/pickle.html
Also, you can think about defining your own __ str __ function and a constructor that will spawn an object from the string (created by __ str __)
精彩评论