我尝试使用pd.read_table()函数将给定文件转换为pandas数据帧。要访问这些文件,我必须使用os.walk()函数遍历目录。然后我循环遍历该数据帧以逐行访问元素并将32行分配给一个数组,然后我将其分配给另一个更大的数组。问题是,当我打印列表时,它不包含任何元素。我认为我在数据框分配声明中出错了,但我不确定。以下是我的代码:
list_of_files = {}
for folder_name in activities:
path = "C:\\Users\\Akarshkb\\Desktop\\cs361hw\\HMP_Dataset\\" +
folder_name
for (dirpath, dirnames, filenames) in os.walk(path):
list_of_subunits = []
for filename in filenames:
if filename.endswith('.txt'):
list_of_files[filename] = os.sep.join([dirpath, filename])
file = open(os.path.join(dirpath, filename), 'r')
df = pd.read_table(file, sep='\s+', header = None)
temp_array = []
subunit_number = 32
current_line_number = 0
for line in df:
current_line_number = current_line_number + 1
temp_array.append(line)
if (current_line_number % subunit_number == 0):
list_of_subunits.append(temp_array)
temp_array.clear()
file.close()
print ((list_of_subunits))
并且print语句就是这样:[[]] 任何帮助将不胜感激。