Phil
Phil

Reputation: 179

Python 3, how to convert a 2D list saved as a string into a list

I have saved some lists to a txt file for later use. I would like to read them in a list.

First row as it appears in csv file in notepad++;

[[781, 828, 2], [844, 896, 2], [902, 950, 2], [957, 1010, 2], [1024, 1070, 2], [1085, 1133, 2], [1143, 1194, 2], [1203, 1251, 2], [1270, 1322, 2], [1330, 1380, 2], [1391, 1436, 2], [1457, 1516, 2], [1525, 1577, 2], [1601, 1665, 2], [1696, 1751, 2], [1760, 1817, 2], [1827, 1880, 2], [1892, 1943, 2], [1955, 2002, 2], [2012, 2062, 2]]

with open('outputSelectorData/type_A.csv', newline='') as a:
readerA = csv.reader(a)

for i in range(count):
        A_list = next(readerA)
        print(A_list)

            
        #f = open("test.csv", "a+")
        #f.write(str(label) + '\n')
        #f.close()

The print A_list outputs the following;

['[[781', ' 828', ' 2]', ' [844', ' 896', ' 2]', ' [902', ' 950', ' 2]', ' [957', ' 1010', ' 2]', ' [1024', ' 1070', ' 2]', ' [1085', ' 1133', ' 2]', ' [1143', ' 1194', ' 2]', ' [1203', ' 1251', ' 2]', ' [1270', ' 1322', ' 2]', ' [1330', ' 1380', ' 2]', ' [1391', ' 1436', ' 2]', ' [1457', ' 1516', ' 2]', ' [1525', ' 1577', ' 2]', ' [1601', ' 1665', ' 2]', ' [1696', ' 1751', ' 2]', ' [1760', ' 1817', ' 2]', ' [1827', ' 1880', ' 2]', ' [1892', ' 1943', ' 2]', ' [1955', ' 2002', ' 2]', ' [2012', ' 2062', ' 2]]']

I have tried a few things to get this into a 2D list(), but thought there may be a simple way someone knows before I start splitting on commas and []'s etc.

Thanks

Upvotes: 0

Views: 45

Answers (2)

Hamza usman ghani
Hamza usman ghani

Reputation: 2243

Use ast (abstract syntax tree) to convert string into list

import ast
with open(r"file.txt") as file:
    for line in file:
        listt=ast.literal_eval(line)
print(listt)

Upvotes: 0

lorenzozane
lorenzozane

Reputation: 1279

You can use literal_eval in ast module as follow:

from ast import literal_eval

text = "[[781, 828, 2], [844, 896, 2], [902, 950, 2], [957, 1010, 2], [1024, 1070, 2], [1085, 1133, 2], [1143, 1194, 2], [1203, 1251, 2], [1270, 1322, 2], [1330, 1380, 2], [1391, 1436, 2], [1457, 1516, 2], [1525, 1577, 2], [1601, 1665, 2], [1696, 1751, 2], [1760, 1817, 2], [1827, 1880, 2], [1892, 1943, 2], [1955, 2002, 2], [2012, 2062, 2]]"

lists = (literal_eval(text))
for el in lists:
    print(el)

or also, use json:

import json

text = "[[781, 828, 2], [844, 896, 2], [902, 950, 2], [957, 1010, 2], [1024, 1070, 2], [1085, 1133, 2], [1143, 1194, 2], [1203, 1251, 2], [1270, 1322, 2], [1330, 1380, 2], [1391, 1436, 2], [1457, 1516, 2], [1525, 1577, 2], [1601, 1665, 2], [1696, 1751, 2], [1760, 1817, 2], [1827, 1880, 2], [1892, 1943, 2], [1955, 2002, 2], [2012, 2062, 2]]"

lists = json.loads(text)
for el in lists:
    print(el)

The output will be absolutely the same:

[781, 828, 2]
[844, 896, 2]
[902, 950, 2]
[957, 1010, 2]
[1024, 1070, 2]
[1085, 1133, 2]
[1143, 1194, 2]
[1203, 1251, 2]
[1270, 1322, 2]
[1330, 1380, 2]
[1391, 1436, 2]
[1457, 1516, 2]
[1525, 1577, 2]
[1601, 1665, 2]
[1696, 1751, 2]
[1760, 1817, 2]
[1827, 1880, 2]
[1892, 1943, 2]
[1955, 2002, 2]
[2012, 2062, 2]

Upvotes: 2

Related Questions