2 回答

TA贡献1780条经验 获得超4个赞
首先,定义一个函数将文件拆分为多个部分。这是一个生成器,它生成一系列行列表:
def split_sections(infile):
"""Generate a sequence of lists of lines from infile delimited by blank lines.
"""
section = []
for line in infile:
if not line.strip():
if section:
yield section
section = []
else:
section.append(line)
if section: # last section may not have blank line after it
yield section
那么你的实际任务相当简单:
with open(path) as infile:
for lines in split_sections(infile):
heading = lines[0].rstrip()
data = np.genfromtxt(lines[1:], usecols=[0,1])
print(heading)
print(data)
添加回答
举报