1 回答
TA贡献1786条经验 获得超11个赞
不处理行的平面列表,而是将其转换为一种结构,可以更轻松地首先“查询”数据,可能会更容易。类似于字典列表,其中每个字典代表一个周期:
cycles = {}
rows = contents.splitlines() # split the `contents` text blob into individual lines
for row in rows[1:]: # the first line in your question is a header - [1:] skips it
row = rows.split() # split each line by whitespace
cycle = cycles.setdefault(row[0], {'id': row[0], 'rows': []}
cycle['rows'].append({'cycle':row[0], 'test_time': row[1], 'current': row[2], ...})
然后,您可以按test_time对它们进行排序:
for key, cycle in cycles.items():
cycles['rows'].sort(key=itemgetter('test_time'))
然后,您可以处理您的数据。每个周期的最后一行:
for key, cycle in cycles.items():
output_row(cycles['rows'][-1])
最近五个周期的行:
for key, cycle in sorted(cycles.items())[:-5]:
output_rows(cycles['rows'])
从4到30中提取行:
for idx in range(4, 31):
cycle = cycles[str(idx)]
output_rows(cycles['rows'])
添加回答
举报