-1

I have a data in the file something like the following:

[['1','1','abc'],['2','2','pqr'],['3','3','xyz']]

I am trying to load this data as it is in the list variable.

with open('abc.txt') as d:
    text = d.readlines()

I got the following output:

>>> text
["[['1','1','abc'],['2','2','pqr'],['3','3','xyz']]"]

Then I tried the command text = d.read() I got the following result:

"[['1','1','abc'],['2','2','pqr'],['3','3','xyz']]"

I am trying to achieve the output as:

>>> text
[['1','1','abc'],['2','2','pqr'],['3','3','xyz']]

Kindly, guide me to fulfill what I want to.

Jaffer Wilson
  • 7,029
  • 10
  • 62
  • 139

1 Answers1

0

You can achieve desired output bywriting this code:

Import ast
with open('abc.txt') as f:
        ls = ast.literal_eval(f.read())

Welcome in advance.