2015-04-15 59 views
-1

您好所有我是新来的Python和真正需要帮助的打开文件,并在文件中选择特定的列

我有一组数据如下 所有大的值是1列和XX.X是列2 19600110 28.6 19600111 28.9 19600112 29.2 19600113 28.6 19600114 28.6 19600115 28.4 19600116 28.6 19600117 28.6

存储为station.txt

我试图让python只提出数据的第一列(19600115等),它被标记为dates.I打开文件,我使用for循环尝试只打开第1列。我不知道我要去的地方错了任何帮助,将不胜感激

高清load_dates(站):
“”“负载站的日期,不包括站的温度数据”“”

f = open(stations[0] + '.txt', 'r') 
#create a for loop and open first column of data which are the dates 
#close the file and return body of dates 
dates = [] 
for line in f: 
    dates.append(lines(7)) 
f.close() 

return dates 
+0

每一行都是“19600110 28.6”,你应该将它分割为.dates = [line.split()[0])。 – sinceq

回答

1
dates = [] 
for line in f: 
    dataItem = line.split() #split by while space by default, as a list 
    date = dataItem[0] #index 0 is the first element of the dataItem list 
    dates.append(date) 
f.close() 

总之,你需要先分割线串,然后选择日期

相关问题