2 回答
TA贡献1859条经验 获得超6个赞
无论文件大小如何,您都可以在此处返回每个打开文件的最后一列:
for xx in range(len(fns_land)):
with open(path) as infile:
lines = infile.readlines()
for line in lines:
result_station.append(line.split(',')[0])
result_date.append(line.split(',')[1])
result_metar.append(line.split(',')[-1])
TA贡献1801条经验 获得超16个赞
首先,我想指出您多次调用 line.split() ,这是不理想的。相反,您可以通过将拆分结果列表分配给变量来拆分该行一次。然后只需从列表中选择项目即可。
但是......实际上我会这样做:
for filename in fns_land:
with open(filename) as infile:
last_line = infile.readlines()[-1]
last_column = last_line.split(',')[-1]
result_metar.append(last_column)
"""
# alternatively 3 lines above could be just be one command, such as
result_metar.append( (infile.readlines()[-1]).split(',')[-1] )
"""
添加回答
举报