[英]Can't Insert array data into QTableWidget PyQt5
I am making a desktop app for scientific purposes, and I am struggling to find info on the topic. 我正在出于科学目的制作台式机应用程序,并且正在努力寻找有关该主题的信息。
I have the code that scans the specific folder of excel files and saves its names and paths 我有扫描excel文件的特定文件夹并保存其名称和路径的代码
sample_directory_2 = []
sample_files_2 = []
for (dirpath, dirnames, filenames) in walk('./Processed'):
filenames = [f for f in filenames if not f[0] == '.']
sample_files_2.extend(filenames)
break
the_dir = "Processed"
paths_2 = [os.path.abspath(os.path.join(the_dir,filename)) for filename in os.listdir(the_dir) if not filename.startswith('.')]
sample_directory_2.append(sample_files_2)
sample_directory_2.append(paths_2)
Further, I have a piece of code, which gets the data from these files 此外,我有一段代码,可以从这些文件中获取数据
processed_info = []
for i in range(len(sample_directory_2[0])):
file_info = []
sample_file_2 = sample_directory_2[0][i]
sample_path_2 = sample_directory_2[1][i]
sample_info_2 = pd.read_excel(ospath(sample_path_2), header = None, sheetname = 3)
sample_info_2 = sample_info_2.iloc[0][0:3]
file_info.append(sample_file_2)
sample_info_2_list = numpy.array(sample_info_2).tolist()
file_info.extend(sample_info_2_list)
processed_info.append(file_info)
It is saved into an array 它保存到数组中
[['sample1 (02:03:2018 14-38).xlsx', 3892.523626752876, 18.64406779661017, 0.6938147012986077],
[['sample1(02:03:2018 14-38).xlsx',3892.523626752876,18.64406779661017,0.6938147012986077],
['sample2 (02:03:2018 14-38).xlsx', 6135.581250460564, 79.66101694915254, 0.6072608909734578],
['sample2(02:03:2018 14-38).xlsx',6135.581250460564,79.66101694915254,0.6072608909734578],
['sample3 (02:03:2018 14-38).xlsx', 3461.456815612303, 5.084745762711865, 0.676008795372024],
['sample3(02:03:2018 14-38).xlsx',3461.456815612303,5.084745762711865,0.676008795372024],
['sample4 (05:03:2018 07-28).xlsx', 6135.581250460564, 79.66101694915254, 0.6072608909734578]]
['sample4(05:03:2018 07-28).xlsx',6135.581250460564,79.66101694915254,0.6072608909734578]]
How to insert these pieces of data into QTableWidget cells, where each list (for example:['sample1 (02:03:2018 14-38).xlsx', 3892.523626752876, 18.64406779661017, 0.6938147012986077]) is inserted as a new row with 4 columns? 如何将这些数据插入QTableWidget单元格,其中每个列表(例如:['sample1(02:03:2018 14-38).xlsx',3892.523626752876,18.64406779661017,0.6938147012986077])作为新行插入4列?
UPD: Found a way to add an item with UPD:找到了一种添加项目的方法
for row in range(len(processed_info)):
for column in range(len(processed_info[row])):
self.clickSample_list.setItem(row, column, QTableWidgetItem(processed_info[row][column]))
but, for some reason, it adds only first items of lists: 但是由于某种原因,它仅添加列表的第一项:
How is that possible? 那怎么可能?
Found a problem. 发现问题。
self.clickSample_list.setItem(row, column, QTableWidgetItem(processed_info[row][column]))
only works with string items, so I replaced it with 仅适用于字符串项目,因此我将其替换为
self.clickSample_list.setItem(row, column, QTableWidgetItem(str(processed_info[row][column])))
easy as it is 如此简单
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.