How do I convert to list of lists after reading from file in python -


this question has answer here:

i append-writing lists text file through iterations using writelines(str(list)). short, use 2 lists illustrate:

list1=['[#1]:', (4, 8, 16, 29), (4, 8, 16, 30), (4, 8, 16, 32)]    list2=['[#2]:', (3, 9, 13, 20), (3, 9, 13, 36), (3, 9, 13, 38)] 

and in text file, have pile of separate lists:

['[#1]:', (4, 8, 16, 29), (4, 8, 16, 30), (4, 8, 16, 32)] ['[#2]:', (3, 9, 13, 20),  (3, 9, 13, 36), (3, 9, 13, 38)]  

when read text file list using readlines(file.txt), single list of string:

["['[#1]:', (4, 8, 16, 29), (4, 8, 16, 30), (4, 8, 16, 32)], ['[#2]:', (3, 9, 13, 20),  (3, 9, 13, 36), (3, 9, 13, 38)]"]  

this expected. want remove ' " ' (quotation marks) @ beginning , end of list can iterate through list , process list[#1], list[#2] etc. expect elementary can't work out. appreaciate if can show me how.

thanks in advance.

have tried pickle module?

it used object serialization , can dump objects (even lists) file , directly read ease.

example -

import pickle lst1 = [1,2,3,4,5] open('test1.bin','wb') f:     pickle.dump(lst1,f)  open('test1.bin','rb') f1:     lst2 = pickle.load(f1)     print(lst2) >> [1,2,3,4,5] 

for appending can use mode ab . example of appending -

lst2 = [6,7,8,9,10] open('test1.bin','ab') f:     pickle.dump(lst2,f)  open('test1.bin','rb') f1:     lst3 = pickle.load(f1)     print(lst3) >> [1,2,3,4,5]     lst4 = pickle.load(f1)     print(lst4) >> [6,7,8,9,10] 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -