1

I have a text file and there is 3 lines on data in it.

[1, 2, 1, 1, 3, 1, 1, 2, 1, 3, 1, 1, 1, 3, 3]
[1, 1, 3, 3, 3, 1, 1, 1, 1, 2, 1, 1, 1, 3, 3]
[1, 2, 3, 1, 3, 1, 1, 3, 1, 3, 1, 1, 1, 3, 3]

I try to open and get data in it.

with open("rafine.txt") as f:
    l = [line.strip() for line in f.readlines()]
    f.close()

now i have list in list. if i say print(l[0]) it shows me [1, 2, 1, 1, 3, 1, 1, 2, 1, 3, 1, 1, 1, 3, 3] But i want to get numbers in it. So when i write print(l[0][0]) i want to see 1 but it show me [

how can i fix this ?

3 Answers3

6

You can use literal_eval to parse the lines from the file & build the matrix:

from ast import literal_eval

with open("test.txt") as f:
    matrix = []
    for line in f:
        row = literal_eval(line)
        matrix.append(row)

print(matrix[0][0])
print(matrix[1][4])
print(matrix[2][8])

result:

1
3
1
rdas
  • 20,604
  • 6
  • 33
  • 46
2
import json

with open("rafine.txt") as f:
   for line in f.readlines():
      line = json.loads(line)
      print(line)
Axeltherabbit
  • 680
  • 3
  • 20
0

The best approach depends on what assumption you make about the data in your text file:

ast.literal_eval

If the data in your file is formatted the same way, it would be inside python source-code, the best approach is to use literal_eval:

from ast import literal_eval

data = [] # will contain list of lists
with open("filename") as f:
    for line in f:
        row = literal_eval(line)
        data.append(row)

or, the short version:

with open(filename) as f:
    data = [literal_eval(line) for line in f]

re.findall

If you can make few assumptions about the data, using regular expressions to find all digits might be a way forward. The below builds lists by simply extracting any digits in the text file, regardless of separators or other characters in the file:

import re

data = [] # will contain list of lists
with open("filename") as f:
    for line in f:
        row = [int(i) for i in re.findall(r'\d+', line)]
        data.append(row)

or, in short:

with open(filename) as f:
    data= [ [int(i) for i in re.findall(r'\d+', line)] for line in f ]

handwritten parsing

If both options are not suitable, there is always an option to parse by hand, to tailor for the exact format:

data = [] # will contain list of lists
with open(filename) as f:
    for line in f:
        row = [int(i) for i in line[1:-1].split(, )]
        data.append(row)

The [1,-1] will remove the first and last character (the brackets), then split(", ") will split it into a list. for i in ... will iterate over the items in this list (assigning i to each item) and int(i) will convert i to an integer.

treuss
  • 1,913
  • 1
  • 15