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()
>Solution :
To count the number of lists in the file, you can read the file line by line and count the lines that start with "[" (assuming that each list starts with "[" and ends with "]"). Here’s the code:
with open("R1E1.txt", "r") as my_file:
count = 0
for line in my_file:
if line.startswith("["):
count += 1
print("Number of lists:", count)
To extract each list separately, you can read the file line by line and append each line to a buffer until you find a line that ends with "]", which marks the end of a list. Once you find the end of a list, you can process the buffer as a JSON array (using the json module), and then reset the buffer to start reading the next list. Here’s the code:
import json
with open("R1E1.txt", "r") as my_file:
buffer = ""
for line in my_file:
if line.endswith("]\n"): # end of list
buffer += line.rstrip()
lst = json.loads(buffer)
print("List:", lst)
buffer = ""
elif line.startswith("["): # start of new list
buffer = line.rstrip()
else: # middle of a list
buffer += line.rstrip()
This code prints each list separately as a Python list of integers. You can replace the print statement with your own processing logic. Note that this code assumes that each list is a valid JSON array (i.e., it contains only integers, and the elements are separated by commas). If your file format is different, you may need to adjust the code accordingly.