2

I am append-writing lists to a text file through iterations using writelines(str(list)). For short, I will use 2 lists to 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 the text file, I have a 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 I read back the text file into a list using readlines(file.txt), I get a single list of a 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 is expected. What I want to do is to remove the ' " ' (quotation marks) at the beginning and end of the list so I can then iterate through the list and process list[#1], list[#2] etc. I expect that this is elementary but I can't work it out. I would appreaciate if someone can show me how.

Thanks in advance.

matt2605
  • 216
  • 3
  • 15

3 Answers3

1

You can eval-uate string as a Python expression:

>>> s = "[1, 2, 3, 4]"
>>> l = eval(s)
>>> l
[1, 2, 3, 4]
dlask
  • 7,705
  • 1
  • 21
  • 26
0

Have you tried the pickle module?

It is used for object serialization , you can dump your objects (even lists) into the file and directly read it back with ease.

Example -

import pickle
lst1 = [1,2,3,4,5]
with open('test1.bin','wb') as f:
    pickle.dump(lst1,f)

with open('test1.bin','rb') as f1:
    lst2 = pickle.load(f1)
    print(lst2)
>> [1,2,3,4,5]

For appending you can use the mode as ab . Example of appending -

lst2 = [6,7,8,9,10]
with open('test1.bin','ab') as f:
    pickle.dump(lst2,f)

with open('test1.bin','rb') as f1:
    lst3 = pickle.load(f1)
    print(lst3)
>> [1,2,3,4,5]
    lst4 = pickle.load(f1)
    print(lst4)
>> [6,7,8,9,10]
Anand S Kumar
  • 76,986
  • 16
  • 159
  • 156
  • Thanks. This looked promising but I need to append the lists into a single file before reopening it. Your code writes a list to file and reopens the same list which is fine but I can't the find the mode for appending (instead of 'wb'). – matt2605 Jun 20 '15 at 05:56
  • Did you try the mode as `ab` ? – Anand S Kumar Jun 20 '15 at 05:58
  • Yes, I have tried 'ab' and again just now but I'm getting meaningless results. For example if I choose 10 lists, only list 3 is appended. There's no trace for 1, 2, 4,5 etc even when printing. – matt2605 Jun 20 '15 at 06:11
  • Are you sure you are trying in a completely new file? Maybe you are trying in an old file that has some junk data? – Anand S Kumar Jun 20 '15 at 06:14
  • Also, just in case you do not want `ab` you can also put your lists into a single list and pickle dump that big list. Example - `lst = [lst1, lst2]` – Anand S Kumar Jun 20 '15 at 06:15
  • I fixed my code to make sure the file is cleared first. Now I only get the first list read back. – matt2605 Jun 20 '15 at 06:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/81054/discussion-between-anand-s-kumar-and-matt2605). – Anand S Kumar Jun 20 '15 at 06:45
  • I can see that file has appended but only the first list is being read back. Also, I need the lists in the format above like: lst = [[lst1], [lst2], [list]]. – matt2605 Jun 20 '15 at 06:47
  • If you are using `ab` mode you will have to keep calling `pickle.load` the amount of lists you want to get back. A sample code would be - `lst = [pickle.load(f1) for i in range(n)]` where n is the amount of lists you want to load – Anand S Kumar Jun 20 '15 at 06:49
  • Solved. Great. My list size is selected by input and it requires adjusting to reflect len=0-to-n. It works now. – matt2605 Jun 20 '15 at 07:06
0

Once you have a list of list, after using any method to remove the quotes. You can use lambda in turning list of lists into a flat list as in below list of lists l and 'm'

l = [[9,8,6],[1,3,2], [4], [5,7]]
reduce(lambda x,y: x+y,l)

results into:

[9, 8, 6, 1, 3, 2, 4, 5, 7]

Similarly for

m = [(4, 8, 16, 29), (4, 8, 16, 30), (4, 8, 16, 32)]
reduce(lambda x,y: x+y,m)

results into:

(4, 8, 16, 29, 4, 8, 16, 30, 4, 8, 16, 32)
Nirmal
  • 1,039
  • 1
  • 13
  • 30