Create a list of groups from text file in python

Viewed 357

I have a text file showing a list of groups separated by a string of dashes.

Example: (The actual file has about 2500 groups, with a random number of names in each group.)

----------
a1
a2
----------
b1
b2
b3
b4
b5
b6
----------
c1
c2
c3

I would like to create a list of lists that looks like:

[['a1','a2']['b1','b2','b3','b4','b5','b6']['c1','c2','c3']]

So basically, a list of groups that contains a list of the names for each group. I can create a create a list of groups that has the correct number of elements in it, but I can't figure out how to create the separate list of names. The 'names' list either ends up having nothing in it, or having everything in it.

This is my code so far:

#!/usr/bin/env python


# vars
key = '----------'
groups = []
names = []

f = open('filelist.txt', 'r' , encoding="utf-8")
file_str = f.readlines()
f.close()


for i in range(len(file_str)):
    if key in file_str[i]: 
        groups.append([])
    if key not in file_str[i]:
        names.append(file_str[i])

print(groups)
print(names)

This nubie could really use some help!

4 Answers

You can use itertools.groupby for the task:

from itertools import groupby

out = []
with open("your_file.txt", "r") as f_in:
    for v, g in groupby(f_in, lambda k: k.startswith("---")):
        if not v:
            out.append(list(map(str.strip, g)))

print(out)

Prints:

[['a1', 'a2'], ['b1', 'b2', 'b3', 'b4', 'b5', 'b6'], ['c1', 'c2', 'c3']]

When you encounter a new line (key variable) you want to push a new empty array into groups. Else, you just append the line:

key = '----------'
groups = []

file_str = ["----------", "a1", "a2", "----------", "b1", "b2", "b3" ]

for i in range(len(file_str)):
    if key in file_str[i]: 
        groups.append([])
    else:
        groups[-1].append(file_str[i])

print(groups)

edit: one more thing, if you are sure the file starts with the key, this code will run fine. if not, then i would add a check if groups is empty to avoid index out of range error.

Alternatively to Andej's "library answer" which might be a little unsatisfying to a beginner, you could also just slightly adapt your existing approach to make it work:

#!/usr/bin/env python


# vars
key = '----------'
groups = []
names = []

f = open('filelist.txt', 'r' , encoding="utf-8")
# I renamed this since a list of lines is not really just a string :)
file_lines = f.readlines()
f.close()

# iterate over each line, (really no need for the index you used)
# cheekily append a key at the end so the final names group gets added aswell
for line in (file_lines + key):
    if key in line:
        if names: # if the current group is not empty, add it
            groups.append(names)
            names = [] # start a new group
    else:
        names.append(line) # append to the current group

print(groups)

Just split the string by the delimiter and then by newline:

key = '----------'
groups = []
names = []

f = open('filelist.txt', 'r' , encoding="utf-8")
file_str = f.read()
f.close()

file_str = ''.join(file_str.split(key)) # splits the file by dashes and joins it back to a string
file_list = file_str.split('\n') # splits line by line
while '' in file_list: # remove empty strings caused by split()
    file_list.remove('')
print(file_list)
Related