如何向这个从CSV文件提取的自动完成函数添加更多行?

2024-09-27 02:21:03 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的硬编码数据:

autocompleteList3 = {
'': ['','','','','','','','','','','','','','','','','',''],
    'two': ['3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', 'test55'],
    'one': ['3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', '3332', '234234', 'test55', 'test55'],
}

我导入的.csv文件的数据项数相同:

with open('boxes2.csv', mode='r') as csv_file:
    csv_reader = csv.reader(csv_file)
    for row in csv_reader:
        autocompleteList3[row[0]] = [row[1], row[2], row[3]]

第一个问题:我不能在这里添加超过三行。。。如果我继续划[4]],我会被切断。钥匙似乎在某种程度上限制了我。这是怎么回事?我需要第1行到第18行

这是我的自动完成功能:

class Window(QtWidgets.QMainWindow, Ui_MainWindow):        
    def __init__(self, cList):
        super().__init__()

        self.setupUi(self)                                 

        self.cList = cList
        self.Input_5.addItems(sorted(cList.keys())) 
        self.Input_5.activated[str].connect(self.onActivatedText2)

@QtCore.pyqtSlot(str)
def onActivatedText2(self, text):
    self.Input_19.setText(self.cList[text][0])
    self.Input_29.setText(self.cList[text][1])
    self.Input_30.setText(self.cList[text][2])
    self.Input_18.setText(self.cList[text][3])
    self.Input_15.setText(self.cList[text][4])
    self.Input_14.setText(self.cList[text][5])
    self.Input_13.setText(self.cList[text][6])
    self.Input_25.setText(self.cList[text][7])
    self.Input_20.setText(self.cList[text][8])
    self.Input_21.setText(self.cList[text][9])
    self.Input_22.setText(self.cList[text][10])
    self.Input_24.setText(self.cList[text][11])
    self.Input_26.setText(self.cList[text][12])
    self.Input_28.setText(self.cList[text][13])
    self.Input_23.setText(self.cList[text][14])
    self.Input_27.setText(self.cList[text][15])
    self.Input_31.setText(self.cList[text][16])
    self.Input_32.setText(self.cList[text][17])
    self.Input_33.setText(self.cList[text][18])

所以当我选择两个或一个时,3332234234等会自动填充我的所有输入。我不能让它与CSV文件超过三个输入工作。实际上,我甚至不想要硬编码的钥匙/对。如果我可以简单地从CSV文件中提取所有18个值并自动完成,那将是理想的。有人知道怎么做吗


Tags: 文件csvtextselfinputinitdefreader
1条回答
网友
1楼 · 发布于 2024-09-27 02:21:03

明白了。所以,我的索引搞砸了:

with open('boxes2.csv', mode='r') as csv_file:
csv_reader = csv.reader(csv_file)
for row in csv_reader:
    autocompleteList3[row[0]] = [row[1], row[2], row[3], row[4], row[5], row[6]]

在确保.csv文件不包含空单元格后,我可以添加更多行。第三排只有一个空牢房把一切都毁了

相关问题 更多 >

    热门问题