Search code examples
wxpythonmixins

listctrl out of range on files with less that 3 fields


If I have a delimited text file with just 2 columns:

"OBJECTID","FULL_ADDRESS"
573,"1001 QUAIL RIDGE RD"
7123,"1000 S 13TH ST"

How can I tell my items to stay in range.
For any other db with more that 2 columns this code runs fine.

def reloadList(self):
    lc = self.GetListCtrl()
    lc.DeleteAllItems()
    self.itemDataMap = {}
    length = 0
    for row in self.sortedlist: 
        self.itemDataMap[length] = row
        length = length + 1            


items = self.itemDataMap.items()
for key, data in items:
    idx = lc.InsertStringItem(sys.maxint, data[0])
    lc.SetStringItem(idx, 0, data[0])
    lc.SetStringItem(idx, 1, data[1])
    lc.SetStringItem(idx, 2, data[2])
    lc.SetItemData(idx, key)
lc.SetColumnWidth(0, wx.LIST_AUTOSIZE)
lc.SetColumnWidth(1, wx.LIST_AUTOSIZE)
lc.SetColumnWidth(2, wx.LIST_AUTOSIZE)

Solution

  • Just remove the following line:

    lc.SetStringItem(idx, 2, data[2])
    

    That line is what sets the 3rd column. Since you don't have 3 columns, you don't need that code.

    You could also do a len(data) and do something like this:

    if len(data) == 3:
        lc.SetStringItem(idx, 2, data[2])
    

    Then you can handle both cases.