答案 0 :(得分:0)
尝试这种方法:
headings = []
with open(filename) as f:
lines = f.readlines()
n_lines = len(lines)
for i, line in enumerate(lines):
if line.startswith("-----") and \
n_lines > i + 2 and iines[i+2].startswith("-----"):
headings.append(lines[i+1])
答案 1 :(得分:0)
如果在迭代时无法检查上一行和下一行,则可以跟踪何时看到虚线。当看到第一条虚线时,您开始添加文本,遇到下一条虚线时,您停止添加文本,例如
select b.item_id, a.price_type, b.price_amount
from table_price_type A
left join table_price_list B on A.price_type_id=B.price_type_id
对于文本:
headings = []
start = 0
with open('/home/usr3/test1.txt') as f:
for ln in f:
# append to heading list
if start == 1:
# when the second dashed line is seen, stop appending
if ln.startswith('---'):
start = 0
continue
headings.append(ln.rstrip())
# first dashed line, indicate to start appending
if ln.startswith('---'):
start = 1
输出为:
------------
h1
-------------
qww
qwe
qw
eqwe
-------------
h2
-------------
qwqw
ee
e
e
e
-------------
h3
-------------