我正在处理一个文本文件,例如:
blahblahblahblahblahblahblah
blahblahblahblahblahblahblah
start
important1a important1b
important2a important2b
end
blahblahblahblahblahblahblah
我想要的是得到一个像
["'important1a', 'important1b'", "'important2a', 'important2b'"]
每条重要的行都被拆分为单独的元素,但它们在一个列表中按行组合在一起。
我已经接近了这个:
import shlex
useful = []
with open('test.txt', 'r') as myfile:
for line in myfile:
if "start" in line:
break
for line in myfile:
if "end" in line:
break
useful.append(line)
data = "".join(useful)
split_data = shlex.split(data)
print split_data
这输出:
['important1a', 'important1b', 'important2a', 'important2b']
行之间没有区别。
如何修改它以区分每一行?谢谢!