求解:从txt文件导入字符序列到tablewidget的每个单元格中失败,结果是把txt文件中所有字符到每一个单元格中了,我想实现的是用空格分开的每个字符串单独导入每个单元格中,代码如下,怎样修改错误?
def kcbopen(self):
kcbname, _ = QFileDialog.getOpenFileName(self, "Open", "", "*.txt;;All Files(*)")
if kcbname != "": # “”为用户取消
self.textEdit.setText(kcbname)
data = []
with open(str(kcbname), 'r') as f:
for i in f:
data.append([j for j in i.split(" ")])
for i in range(self.tableWidget.rowCount()):
for n in range(self.tableWidget.columnCount()):
for s in data:
self.tableWidget.item(i, n).setText(str(s))
出错动画演示如下: