打开文件并选择文件中的特定列

时间:2015-04-15 02:36:29

标签: python

大家好我是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只显示标记为dates的第一列数据(19600115等)。我打开文件,我使用for循环尝试只打开第1列。我不确定在哪里出错我会非常感谢任何帮助

def 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

1 个答案:

答案 0 :(得分: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()

总之,您需要先拆分行字符串,然后选择日期