我需要将所有numpy文件合并到一个numpy文件中,然后将该文件转换为二进制文件。我首先尝试创建两个不同的列表文件,我将文件名称合并到两个不同的列表中。 我的包含numpy文件的文件夹如下所示:
File1_Trace1
File2_Trace2
File2_Trace3
我尝试使用此代码:
import numpy as np
import sys
import os
# be sure to pass numbers on command line
if len(sys.argv)==3:
start_index, stop_index = int(sys.argv[1]), int(sys.argv[2])
else:
# or just use
start_index, stop_index = 1, 1000
path = r'Y:\My_Scripts'
File_path= r'Y:\My_Scripts\1000_Traces'
lines_npy = [line.rstrip('\n') for line in open(os.path.join(path, 'Traces.list_npy'))]
lines_bin = [line.rstrip('\n') for line in open(os.path.join(path, 'Traces.list_bin'))]
all_traces = np.zeros((stop_index - start_index + 1, 8000))
for x in range(start_index, stop_index):
trace = np.load(os.path.join(path, lines_npy[x]))
trace.astype('float32').tofile(os.path.join(File_path, lines_bin[x]))
all_traces[x] = trace
all_traces.astype('float32').tofile(os.path.join(path, 'TRACES.bin'))
它不起作用的问题,首先它问我两个列表文件。然后它让我误解了这个错误:
trace = np.load(os.path.join(path, lines_npy[x]))
IndexError: list index out of range
答案 0 :(得分:0)
问题在于索引lines_npy
; x
是无效值,可能太大了。要跟踪这一点,您需要关注代码的这一部分:
# be sure to pass numbers on command line
if len(sys.argv)==3:
start_index, stop_index = int(sys.argv[1]), int(sys.argv[2])
else:
# or just use
start_index, stop_index = 1, 1000
path = r'Y:\My_Scripts'
File_path= r'Y:\My_Scripts\1000_Traces'
lines_npy = [line.rstrip('\n') for line in open(os.path.join(path, 'Traces.list_npy'))]
print(len(lines_npy)
print(start_index, stop_index)
for x in range(start_index, stop_index):
print(lines_npy[x]))
默认stop_index
表示您可以拥有1000个文件?这有意义吗?