1 回答
TA贡献1785条经验 获得超8个赞
因此,您最初可以简化循环,如下所示:
with open("file.xvg", "r") as f:
for i, line in enumerate(f):
if i < 22:
# Skip up to line 22
continue
line = line.strip().split()
# It isn't clear what you want to append to `res`
if float(line[1]) >= 9.5:
print("P")
break
elif float(line[1]) <= 5.9:
print("R")
break
else:
print("N")
这将分析文件,而不是将其读取到列表中并分析列表。break 语句将您带出通过文件的循环。
如果目录中有许多文件,则可以像这样阅读它们:
from pathlib import Path
for file in Path("/path/to/directory").rglob("*.xvg"):
with file.open("r") as f:
for i, line in enumerate(f):
# Same logic as above
pass
添加回答
举报