如何从数组移动到python中的一组文件?

时间:2017-04-26 10:37:15

标签: python

我尝试在一组数据中进行一些分析,我找到了一个有用的教程来进行这些分析。本教程需要使用一个包含来自一组文件的连接数据的文件

tempTraces = np.load(r'C:\\Users\\user\\2016.06.01-09.41.16_traces.npy')
    for i in range(len(tempTraces)):
        HW = tempHW[i]
        print (tempTraces[i])
        tempTracesHW[HW].append(tempTraces[i])
    print(tempTracesHW)

    # Switch to numpy arrays
    tempTracesHW = [np.array(tempTracesHW[HW]) for HW in range(9)]
    print (len(tempTracesHW[8]))

结果是:

[ array([[ 0.06835938,  0.0390625 ,  0.07519531, ...,  0.0546875 ,
         0.08886719,  0.02734375],
       [ 0.06542969,  0.04199219,  0.07714844, ...,  0.06152344,
         0.07324219,  0.09472656],
       [ 0.06640625,  0.04101562,  0.07714844, ...,  0.10742188,
         0.13574219,  0.03222656],
       [ 0.06445312,  0.03613281,  0.07519531, ...,  0.14160156,
         0.1171875 ,  0.14257812],
       [ 0.06347656,  0.04003906,  0.07519531, ...,  0.05566406,
         0.08300781,  0.02539062],
       [ 0.06542969,  0.0390625 ,  0.08105469, ...,  0.03222656,
         0.06738281,  0.07714844]]), 

 array([[ 0.06640625,  0.04199219,  0.07519531, ...,  0.02148438,
         0.0859375 ,  0.12695312],
       [ 0.06640625,  0.04199219,  0.078125  , ...,  0.08886719,
         0.02734375,  0.02734375],
       [ 0.06738281,  0.04394531,  0.07910156, ...,  0.06347656,
         0.08496094,  0.02050781],
       ..., 
       [ 0.0546875 ,  0.03320312,  0.07519531, ...,  0.14355469,
         0.0390625 ,  0.06738281],
       [ 0.06152344,  0.03808594,  0.07421875, ...,  0.04882812,
         0.04296875,  0.09082031],
       [ 0.06640625,  0.03515625,  0.07617188, ...,  0.14355469,
         0.04003906,  0.06542969]]), ...]

len(tempTracesHW[8]) = 7

在我的情况下数据没有连接,我更喜欢不连接它们,因为它需要大量的内存,它对我来说是一个大问题,所以我尝试转换这个代码,以便使用它与一组文件:

path ='C:\\Users\\user\\Traces'
traces= os.listdir(path)
for i in range(len(traces)):
   HW = tempHW[i]
   tempTracesHW.append([np.load(os.path.join(path, trace)) for trace in traces])
print (tempTracesHW)

我的结果是错误的:

[[], [], [], [], [], [], [], [], [], array([[ 0.01437869,  0.01506449,  0.01579909, ...,  0.04166172,
         0.0417285 ,  0.04172079]], dtype=float32), array([[ 0.01437869,  0.01506449,  0.01579909, ...,  0.04166172,
         0.0417285 ,  0.04172079]], dtype=float32), array([[ 0.01437869,  0.01506449,  0.01579909, ...,  0.04166172,
         0.0417285 ,  0.04172079]], dtype=float32),...


len(tempTracesHW[8])=0

我真的需要帮助。

1 个答案:

答案 0 :(得分:0)

您可能希望过滤所获得的文件列表 来自os.listdir()。并非每个文件都包含有用的数据。 考虑fnmatch以匹配有意义的文件名。

from fnmatch import fnmatch 

path ='C:\\Users\\user\\Traces'
traces= [
    file_name for file_name in os.listdir(path) 
              if fnmatch(file_name, "*.dat")
]
for i in range(len(traces)):
   HW = tempHW[i]
   tempTracesHW.append([np.load(os.path.join(path, trace)) for trace in traces])
print (tempTracesHW)

此外,您可以尝试:

for i, trace in enumerate(traces):
   HW = tempHW[i]  # unclear what HW does, assume influence on np.load()
   tempTracesHW.append(np.load(os.path.join(path, trace)))