2 回答

TA貢獻(xiàn)1780條經(jīng)驗 獲得超4個贊
首先,定義一個函數(shù)將文件拆分為多個部分。這是一個生成器,它生成一系列行列表:
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
那么你的實際任務(wù)相當(dāng)簡單:
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)
添加回答
舉報