I am trying to read a text file and output it as a list of lists

Question:

I am trying to read a text file and output it as a list of lists

text file is formatted like this

Frank Smith
5 5 0 0 100 15
Steven Jones
12 0 6 6 10 6
Barry Player
6 2 0 4 20 10
Scott Morrison
7 4 0 3 300 15
Tim Mcgrath
3 0 2 1 50 1

and I need the list imported to display like this

a_list = [['Frank Smith', 5, 5, 0, 0, 100, 15],['Steven Jones', 12, 0, 6, 6, 10, 6],['Barry Player', 6, 2, 0, 4, 20, 10],['Scott Morrison', 7, 4, 0, 3, 300, 15],['Tim Mcgrath', 3, 0, 2, 1, 50, 1]]

below is what I have tried but it doesn’t keep the names as a single and puts quotes on every thing

a_file = open("list.txt", "r")


list_of_lists = []

for line in a_file:

  stripped_line = line.strip()

  line_list = stripped_line.split()

  list_of_lists.append(line_list)


a_file.close()

print(list_of_lists)```

Asked By: firstyearpleb

||

Answers:

with open('list.txt', 'r') as f:
    text = f.read()

result = []
for line in text.split('n'):
    row = line.split(' ')
    if len(row) == 2:
        result.append([' '.join(row)])
    else:
        row = [int(e) for e in row]
        result[-1].extend(row)

print(result)

prints

[['Frank Smith', 5, 5, 0, 0, 100, 15],
 ['Steven Jones', 12, 0, 6, 6, 10, 6],
 ['Barry Player', 6, 2, 0, 4, 20, 10],
 ['Scott Morrison', 7, 4, 0, 3, 300, 15],
 ['Tim Mcgrath', 3, 0, 2, 1, 50, 1]]

Explanation: Read the file into a string, then separate each line of text into a separate string by splitting the string at newlines, then separate each word of the line into a separate string by splitting the line at whitespaces. If there are two words in the line, we know it is a name and add a new list to our result – which is a list of lists – containing the name as a string. Otherwise, we know that it is the numbers, and convert them from strings to integers and add them to the last list in the result.

Answered By: Michael Hodel

I saw your code and I think there are some modifications you need to do to print in the given format.Basically What your want to print is if line is odd you just add that string to a list and if line is even you split them and convert them into a list of integer and append it to previous list This code is working fine . Kindly Check It 🙂

a_file = open("list.txt", "r")


list_of_lists = []

count = 0

index = 0

for line in a_file:
    
    if count%2 == 0:

        stripped_line = line.strip()
        if stripped_line == "":
            continue
        list_of_lists.append([stripped_line])
        
    else:
        stripped_line = line.strip()   
        line_list = stripped_line.split()
        for i in range(len(line_list)):
            line_list[i] = int(line_list[i])

        list_of_lists[index].extend(line_list)
        index+=1
    count+=1


a_file.close()

print(list_of_lists)

It Prints-

[['Frank Smith', 5, 5, 0, 0, 100, 15], ['Steven Jones', 12, 0, 6, 6, 10, 6], ['Barry Player', 6, 2, 0, 4, 20, 10], ['Scott Morrison', 7, 4, 0, 3, 300, 15], ['Tim Mcgrath', 3, 0, 2, 1, 50, 1]]
Answered By: Vivek Maddeshiya
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.