我有一个文本文件,如下所示:
[START]
hey there
how are you
[END]
[START]
i am great
long time no see
[END]
[START]
yeah
busy lives
[END]
如果我想将分隔线[START]和[END]之间的所有行附加到列表中,我该怎么做?
我尝试这样做:
f = open(filename)
f.readline()
lst = []
while not (line == '[START]'):
lst.append(line)
return lst
答案 0 :(得分:0)
flutter create .
您可以将/ n或[START]或[END]替换为line.replace(“ / n”,“”)
答案 1 :(得分:0)
这可以通过列表理解来简单地完成:
with open(filename) as file:
return [line.rstrip('\n') for line in file if line.startswith('[START]') or line.startswith('[END]')]