import numpy as np
import urllib2
import csv
txtFile = csv.reader(open("delnorte.dat.txt", "r"), delimiter='\t')
fields = 5
records = []
for row, record in enumerate(txtFile):
if (len(record) != fields or record[0]=='#'):
pass
# print "Skipping malformed record or comment: {}, contains {} fields ({} expected)".format(record,len(record),fields)
else:
if record[2][0:4] == '2005':
# assuming you want columns 3 & 4 with the first column indexed as 0
records.append([int(record[:][3]), record[:][4]] )
# if desired slice the list of lists to put a single column into a numpy array
npData = np.asarray([ npD[0] for npD in records] )
我同意使用csv模块。我修改了这个答案:reading csv files in scipy/numpy in Python 适用于你的问题。不确定您是否需要numpy数组中的数据,或者列表是否足够。在
您可以为特定列提供自定义的转换函数到
loadtxt
。由于您只对年份感兴趣,我使用
lambda
-函数来分割-
上的日期,并将第一部分转换为int
:要过滤年度
^{pr2}$2005
,可以在numpy中使用logical indexing:你不应该使用NumPy.loadtxt文件要读取这些值,您应该使用^{} module 来加载文件并读取其数据。在
相关问题 更多 >
编程相关推荐