我无法从目录中读取多个csv文件。
代码在第一个文件运行时运行良好,但是我在第二个文件上出错了。以下是相关的代码部分。 (但是对于这个问题,一些是不必要的,我只是把它留在了所以它更有意义):
def get_data(filename):
'''function to read the data form the input csv file to use in the analysis'''
with open(filename, 'r') as f:
reader = csv.reader(f,delimiter=',')
#returns all the data from the csv file in list form
return list(reader)
path='C:\Users\AClayton\Desktop\AW189 Data'
for infile in glob.glob(os.path.join(path, '*.csv')):
# infile stores the complete path of the file
print "Current File Being Processed is: " + infile
#use split to seperate the path and name of the file
(PATH, FILENAME) = os.path.split(infile)
print " PATH is " + PATH
print " FILENAME is " + FILENAME
all_data=[]
#adds the data from the csv file to a blank list so it can be operated on
all_data.extend(get_data(infile))
#Create array so numerical operations may be performed
arrdata=np.array(all_data)
current_tracks=establish_current_tacks(arrdata,nb)
rel_values=relative_track(current_tracks,nb)
avg_rel_track=averaged_rel_track(navg, rel_values, nb)
avg_rel_track_nan=avg_rel_track_nan(avg_rel_track)
sd_rel_track_sum=sd_of_rel_track(navg, rel_values, nb)
sd_index=sd_index(sd_rel_track_sum)
这是我的错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
C:\Users\AClayton\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.0.3.1262.win-x86_64\lib\site-packages\IPython\utils\py3compat.pyc in execfile(fname, glob, loc)
174 else:
175 filename = fname
--> 176 exec compile(scripttext, filename, 'exec') in glob, loc
177 else:
178 def execfile(fname, *where):
C:\Users\AClayton\Desktop\currentmethod06_09_2013work-1.py in <module>()
204 rel_values=relative_track(current_tracks,nb)
205 avg_rel_track=averaged_rel_track(navg, rel_values, nb)
--> 206 avg_rel_track_nan=avg_rel_track_nan(avg_rel_track)
207 sd_rel_track_sum=sd_of_rel_track(navg, rel_values, nb)
208 sd_index=sd_index(sd_rel_track_sum)
TypeError: 'numpy.ndarray' object is not callable
我不确定导致此问题的原因。它似乎不是将函数作为函数读取,而是作为早期的数组? 非常感谢任何建议
答案 0 :(得分:1)
这很奇怪:
avg_rel_track_nan=avg_rel_track_nan(avg_rel_track)
你可能意味着:
avg_rel_track_nan=averaged_rel_track_nan(avg_rel_track)
我无法确定发生了什么,因为很多代码依赖于其他一些代码,但是您将(假设的)函数调用的结果分配给此函数的名称。我猜这不是你的意思。
答案 1 :(得分:0)
尝试:
def get_data(filename):
'''function to read the data form the input csv file to use in the analysis'''
reader = [] # Just in case the file open fails
with open(filename, 'r') as f:
reader = csv.reader(f,delimiter=',')
#returns all the data from the csv file in list form
#f.close() # May need to close the file when done
return list(reader) # only return the reader when you have finished.
我认为问题的一部分是你从with,(并没有关闭文件)中返回