在python中出现符号后向列表中添加内容

在python中出现符号后向列表中添加内容,python,grep,Python,Grep,我有以下数据 1 abc > 2 def efg > 3 hij jkl > 4 mno 5 pqr stu 我希望在每次出现“>”和数字“3”后将所有内容添加到列表中 输出应该是 [[abc],[[def],[efg]],[[hij],[jkl]],[mno],[[pqr],[stu]]] 假设项目在列表L中 >>> from itertools import groupby >>> [list(g) for k, g in group

我有以下数据

1
abc
>
2
def
efg
>
3
hij
jkl
>
4
mno
5
pqr
stu
我希望在每次出现“>”和数字“3”后将所有内容添加到列表中

输出应该是

[[abc],[[def],[efg]],[[hij],[jkl]],[mno],[[pqr],[stu]]]

假设项目在列表L中

>>> from itertools import groupby
>>> [list(g) for k, g in groupby(L, ">0123456789".__contains__) if not k]
[['abc'], ['def', 'efg'], ['hij', 'jkl'], ['mno'], ['pqr', 'stu']]

它的格式不完全正确,因为您的格式不是有效的python。但是应该开始了

如果使用文件存储信息,可以使用以下方法:

result = []

with open("data.txt", "r") as f:

    # Read file line by line
    for line in f:
        line = line.strip()

        # Ignore '>' or numbers         
        if line == '>' or line.isdigit():
            continue
        result.append([line])

print(result)

因此,这不是编码服务。请张贴您的代码并提出具体问题。