I have a text file in the following format
>heading
A B C D
E F G H
I J K L
>heading
M N O P
Q R S T
>heading
U V W X
Y Z a b
...
...
Each line has the same number of strings except those with the heading and there is not the same number of lines between each ">heading" line.
I would like to read the text file in order to create a Numpy array in the following format:
[[[A, B, C, D], [E, F, G, H], [I, J, K, L]],
[[M, N, O, P], [Q, R, T, S]],
[[U, V, W, X], [Y, Z, a, b]]]
I thought of checking whether a line is a ">heading" and creating a list of lines that follow it until the next ">heading". Problem is I do not know how to merge those lists into a larger list without it becoming one simple list again.
I have tried the following:
groups = []
with open('textfile.txt', 'r') as f:
test = '>heading
'
smallgroup = [test]
for line in f:
if line != test:
smallgroup.append(line)
groups.append(smallgroup)
else:
groups.extend(smallgroup)
question from:
https://stackoverflow.com/questions/65902303/python-separate-text-file-into-list-of-lists-depending-on-heading 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…