0

現在、SNMP データを分析するための Python スクリプトに取り組んでいます。csv ファイルを読み取り、CheckListBox に編成された CheckBoxes にヘッダーを表示する機能があります。最初の項目を削除したいのですが、そうすると 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 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)
4

2 に答える 2

0
 for row in reader:
            #Inserts each field into CheckListBox as an item;
            #self.SNMPCheckListBox.InsertItems(row,0)
            listItems.append(row)
            break

ブレークを使用したため、リストには項目が 1 つしかありません。したがって、そのアイテムを削除すると、CheckListBox に入力するものは何もありません。

それが最初の項目であると 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 リストに 2 番目以降の項目のみを追加することを意味します。

于 2015-04-07T14:48:01.453 に答える
0

roxan のおかげで、エラーを確認した後、問題を解決できました。行の各列をアイテムにするのではなく、csv行を1つのアイテムとしてリストに保存していました。これが私の修正です:

#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)
于 2015-04-07T15:09:43.293 に答える