0

I have a large text file that includes so many lists. Each list includes random numbers as follows. Test file:

[-1460,-1460,-1460,-1460,-1460,0,-1460,0,-1460,-1460,-1460,45,-1460,-1460,-1460,-1460,-1460,-1460]
[250,-1250,36,-1250,-1250,33,-1250,-1250,-1250,-1250,-1250,-1250,-1250,-490,-1243,-1250,-1250,-1250,-1250,-1250,33,-1250,33,-1250,-1250,-1250,-1250,-1250,-1250,33,-496,-1243,-1250,33,-1250,-1246,-1250,-1250,-1250,-1250,35,-1250,-1250,33,-1250,-1250,-1250,-1250,-1250,-1250,-1250,-1250,33,-1250,-1250,-1250,-1250,-525,-1250,33,-259,-1250]
[2,-1232,34,34,34,0,0,-1232,-1232,-1232,-1232,34,34,-1232,-1232,-1232,-1232,-1232,-1232,-1232,-1232,34,34,-1232,34,-1232,34,34,-1232,-1232,-1232,39,-1232,34,-1232,-1232,-1232,34,-1232,0,0,34,-1232,-1232,-1232,-1232,-1232,517,0,34,34,34,-1232,-1232,-1232,-1232,-1232,-1232,34,-1232,-1232,-1232,34,-1232,34,-1232,34,-1232,34]
......

First, I want to count how many lists are there in the text file. Then, I want to extract every list separately for further processing.

I created a code to read the text file, but it read the whole file contents as a one-string variable, as follows:

# opening the file in read mode
my_file = open("R1E1.txt", "r")

# reading the file
data = my_file.read()
Mortz
  • 4,654
  • 1
  • 19
  • 35
Mohsen Ali
  • 655
  • 1
  • 9
  • 30

2 Answers2

0

You can extract the lists from the data string variable with some regex matching the square brackets -

import re
list_of_lists = []
for list_entry in re.split(r'\[|\]|\ +', s): #This splits on either a opening bracket or a closing bracket or more than 1 space
    if list_entry.strip():
        list_of_lists.append(list_entry.split(','))
Mortz
  • 4,654
  • 1
  • 19
  • 35
0

In order to determine the number of lists in the file you just need to count the number of left brackets or, as shown here, by counting the number of elements in list_of_lists

Given that the lists could span multiple lines you can effectively join those lines by removing all newline characters. Then separate the lists with comma. (You also need to account for an unwanted trailing comma). You can then create a list of lists by prepending the data with [ and appending ] before passing to literal_eval

from ast import literal_eval

with open("R1E1.txt") as f:
    contents = f.read()
    contents = contents.replace("\n", "")
    contents = contents.replace(']', '],').rstrip(',')
    list_of_lists = literal_eval(f'[{contents}]')
    print(f'List count = {len(list_of_lists)}')
    print(list_of_lists)

Given a file that looks like this:

[1,2,3,
4]
[8,9]

...the output of this code would be:

[[1, 2, 3, 4], [8, 9]]
DarkKnight
  • 19,739
  • 3
  • 6
  • 22