好的......所以我正在尝试构建一个允许我将TSV文件导入矩阵的python脚本。然后取前三列.. col-1是day,2是minTemp,3是maxTemp。我想从col2中获取minTemp以及相关日和maxTemp以及相关日。这是来自NOAA文件。所以顶部有网站信息等,我想要的实际数据从第9行的标签开始。无法弄清楚如何获得关联日。我也因为这个问题而错过了wazoo。我已经两年没碰过Python所以请原谅我。
import csv
inFile = 'weather1.txt'
with open(inFile,'r') as tsv:
for i in range (8):
inFile.next()
weatherOutput = [line.strip().split('\t') for line in tsv];
for row in WeatherOutput:
weatherOutput.append(row);
print (WeatherOutput)
day = (col[1])
maxTemp = (col[2])
minTemp = (col[3]);
minimumTemp = min(col[3])
maximumTemp = max(col[2])
print (minimumTemp, maximumTemp);
答案 0 :(得分:0)
您已经拆分成列表do do zip以转置列表:
weather = [["monday",22,10],["tuesday",25,9]]
days, maxTemps ,minTemps = zip(*weather)
print (days, maxTemps, minTemps)
('monday', 'tuesday') (22, 25) (10, 9)
索引也会启动0
而不是1
,因此如果您尝试访问前三个元素,则col[0],col[1] and col[2]
追加到你正在迭代的列表意味着列表不断增长,所以你将无限循环,你的代码永远不会再进一步:
for row in WeatherOutput:
weatherOutput.append(row);
print (WeatherOutput)