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只显示第一列数据(19600115等),该列标记为日期。我不知道我哪里出了问题。如果您能提供帮助,我将不胜感激 def加载日期(站点):Python 打开文件并选择文件中的特定列,python,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只显示第一列数据(19600115等),该列标记为日期。我不知道我哪里出了问题。
“”“加载站点日期并排除站点温度数据”“” 总之,您需要首先拆分行字符串,然后选择日期每行为“19600110 28.6”,您应该拆分它。日期=[line.split()[0]表示f中的行]
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
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()