Python:需要从列表中删除第一项,然后填充wx.CheckListBox
我目前正在编写一个python脚本来分析SNMP数据。我有一个函数,可以读取csv文件,并在复选框中显示标题。我想删除第一项,但这样做会使我的CheckListBox无法填充,我无法找出原因。代码如下:Python:需要从列表中删除第一项,然后填充wx.CheckListBox,python,list,checklistbox,Python,List,Checklistbox,我目前正在编写一个python脚本来分析SNMP数据。我有一个函数,可以读取csv文件,并在复选框中显示标题。我想删除第一项,但这样做会使我的CheckListBox无法填充,我无法找出原因。代码如下: #Generates CheckBoxList with fields from csv (first row) def onSNMPGen(self,e): #reads in first row of csv file; this snmpPaths[0] will likely c
#Generates CheckBoxList with fields from csv (first row)
def onSNMPGen(self,e):
#reads in first row of csv file; this snmpPaths[0] will likely cause issues with multiple paths -- fix later
listItems = []
print "*** Reading in ", self.snmpPaths[0], "....."
with open(self.snmpPaths[0], 'r') as f: #remember to close csv
reader = csv.reader(f)
print "*** Populating Fields ..... "
for row in reader:
#Inserts each field into CheckListBox as an item;
#self.SNMPCheckListBox.InsertItems(row,0)
listItems.append(row)
break
f.close()
#Need to remove 'Time' (first item) from listItems
#listItems.pop(0) # this makes it so my CheckListBox does not populate
#del listItems[0] # this makes it so my CheckListBox does not populate
for key in listItems:
self.SNMPCheckListBox.InsertItems(key,0)
因为您使用了break,所以列表中只有一项。因此,当您删除该项时,没有任何内容可以填充您的复选框
如果您100%确定这是第一项,则可以重写循环,如:
for row in reader[1:]:
#Inserts each field into CheckListBox as an item;
#self.SNMPCheckListBox.InsertItems(row,0)
listItems.append(row)
reader[1:]表示您只需在listItems列表中添加第二个项目。多亏了roxan,我才能够在看到错误后修复问题。我将csv行存储为列表中的一项,而不是将行的每一列都作为一项。以下是我的解决方案:
#Generates CheckBoxList with fields from csv (first row)
def onSNMPGen(self,e):
#reads in first row of csv file; this snmpPaths[0] will likely cause issues with multiple paths -- fix later
listItems = []
print "*** Reading in ", self.snmpPaths[0], "....."
with open(self.snmpPaths[0], 'r') as f: #remember to close csv
reader = csv.reader(f)
print "*** Populating Fields ..... "
for row in reader:
listItems = row break
f.close()
del listItems[0] #Time is always first item so this removes it
self.SNMPCheckListBox.InsertItems(listItems,0)
好的,我现在明白了。按照我脚本其余部分的工作方式,该修复将导致其他问题。也许我想解析列表中的一个项目并以这种方式将其删除,或者找到忽略/删除csv文件第一列的方法谢谢您指出我将csv行存储为一个项目,而不是多个项目。我已经想出了一个解决办法,我将张贴
#Generates CheckBoxList with fields from csv (first row)
def onSNMPGen(self,e):
#reads in first row of csv file; this snmpPaths[0] will likely cause issues with multiple paths -- fix later
listItems = []
print "*** Reading in ", self.snmpPaths[0], "....."
with open(self.snmpPaths[0], 'r') as f: #remember to close csv
reader = csv.reader(f)
print "*** Populating Fields ..... "
for row in reader:
listItems = row break
f.close()
del listItems[0] #Time is always first item so this removes it
self.SNMPCheckListBox.InsertItems(listItems,0)