我有一个简单的函数来从一个文本块中删除行(从我想要清理的另一个程序输出)。出于某种原因,它删除了一些行而不是其他行。
def remover(lst):
for no, line in enumerate(lst):
if line.startswith('[') or line.startswith('{'):
print(no, line)
lst.remove(line)
return lst
我有一个如下字符串:
msg = '''Beginning process for 09/06/2016
[datetime.date(2016, 9, 6), datetime.date(2016, 9, 7)]
[(48614, 'snacks', datetime.datetime(2016, 9, 6, 8, ...))]
[{'recipe': 'pb&j', 'discharge_times': [datetime.da ...]}]
{'recipe': 'mayo', 'discharge_times': [datetime.dat ...]}
{'RX 360': 1402.0, 'QR 1038': 69255.0, 'FZ 790': 52 ...}
Order created: 580
1 order was successfully created.
1 order was also successfully processed.'''
我使用splitlines
方法将该函数作为列表传递给函数:
remover(msg.splitlines())
输出这个:
1 [datetime.date(2016, 9, 6), datetime.date(2016, 9, 7)]
2 [(48614, 'snacks', datetime.datetime(2016, 9, 6, 8, ...))]
4 {'recipe': 'mayo', 'discharge_times': [datetime.dat ...]}
并返回此项(无法删除第3行和第5行):
'''Beginning process for 09/06/2016
[{'recipe': 'pb&j', 'discharge_times': [datetime.da ...]}]
{'RX 360': 1402.0, 'QR 1038': 69255.0, 'FZ 790': 52 ...}
Order created: 580
1 order was successfully created.
1 order was also successfully processed.'''
如果我多次通过该函数运行它,它最终会删除所有行。它也适用于不同的列表,但出于某种原因不适用于此。
我错过了一些简单的东西吗? 为什么这不能按预期工作?