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!