'How to write a list of lists with tuples to the file?

I have a list:

[[(0.2419758865982183, 'silma_04_430_1.jpg'),
(0.23656135377750184, 'silma_04_430_2.jpg'),
(0.23116990818696198, 'silma_04_430_3.jpg'),
(0.24478668200193482, 'silma_04_430_4.jpg'),
(0.23331325657735746, 'silma_04_430_5.jpg')],
[(1.5642434041028273, 'silma_08_470_1.jpg'),
(1.4337583162644905, 'silma_08_470_2.jpg'),
(1.5824875174894668, 'silma_08_470_3.jpg'),
(1.4201680240607917, 'silma_08_470_4.jpg'),
(1.663849436264644, 'silma_08_470_5.jpg')]]

The aim is to write it to the txt file. And read it back to the list.

I tried this:

with open('list_of_sharpness.txt', 'w+') as f:     
    for items in list_of_sharpness:
        f.write('\n'.join('{} {}'.format(x[0],x[1]) for x in items))
f.close()

The output is wrong. I am confused how to cope with a list of lists.

['0.2419758865982183 silma_04_430_1.jpg\\n',
'0.23656135377750184 silma_04_430_2.jpg\\n',
'1.5837012196929814 silma_30_694_5.jpg1.5642434041028273 silma_08_470_1.jpg\\n',
'1.4337583162644905 silma_08_470_2.jpg\\n]'


Solution 1:[1]

A .txt file is fine for a simple list, but you'll need some extra code to make it work with your data structure, both for writing and reading it. For more complex data structures you could look into storing as JSON instead of txt.

Do note that JSON doesn't have a data type for tuples, so the example below makes lists out of them.

import json

data = [
    [(0.2419758865982183, 'silma_04_430_1.jpg'),
        (0.23656135377750184, 'silma_04_430_2.jpg'),
        (0.23116990818696198, 'silma_04_430_3.jpg'),
        (0.24478668200193482, 'silma_04_430_4.jpg'),
        (0.23331325657735746, 'silma_04_430_5.jpg')],
    [(1.5642434041028273, 'silma_08_470_1.jpg'),
        (1.4337583162644905, 'silma_08_470_2.jpg'),
        (1.5824875174894668, 'silma_08_470_3.jpg'),
        (1.4201680240607917, 'silma_08_470_4.jpg'),
        (1.663849436264644, 'silma_08_470_5.jpg')]
]

with open("list_of_sharpness.json", 'w') as outfile:
    json.dump(data, outfile)

with open("list_of_sharpness.json", 'r') as infile:
    imported_data = json.load(infile)

print(imported_data)

>>>
[
    [[0.2419758865982183, 'silma_04_430_1.jpg'],
        [0.23656135377750184, 'silma_04_430_2.jpg'],
        [0.23116990818696198, 'silma_04_430_3.jpg'],
        [0.24478668200193482, 'silma_04_430_4.jpg'],
        [0.23331325657735746, 'silma_04_430_5.jpg']],
    [[1.5642434041028273, 'silma_08_470_1.jpg'],
        [1.4337583162644905, 'silma_08_470_2.jpg'],
        [1.5824875174894668, 'silma_08_470_3.jpg'],
        [1.4201680240607917, 'silma_08_470_4.jpg'],
        [1.663849436264644, 'silma_08_470_5.jpg']]
]

Solution 2:[2]

I don't know if this is going to work but I had a similar problem and this was my solution.

import pickle #it is part of the standard library so you don't need it install it

with open('thetextfile.txt', 'wb') as f:
    pickle.dump(listname, f)

Pickle is unsafe for untrusted data so you may also want to check out json which is also a part of the standard library

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nick
Solution 2 Dodu