Reputation: 3775
I have a wx.ListView box named lvActions that I add data to using code similar to
self.lvActions.Append([datetime.datetime.now(),filename,"Moved"])
What I want to do is when I am all done with my actions and have a list, I want to output the full contents of this file to a logfile. This is how I tried to do it
logfile = open(logFullPath, "a")
for events in self.lvActions:
logfile.write(events)
logfile.close()
The error I get back is
TypeError: 'ListView' object is not iterable
If a ListView is not iterable, how can I dump its contents to a file?
Upvotes: 0
Views: 334
Reputation: 10172
As you have pointed out, the listview itself is not iterable in the way that you'd like. As with most of the wx widgets, you need to get a count of the items in the widget and then ask for the text of the item at that location. Since you are working with a listview (which is derived from listctrl), you will have to get the text for each column individually
logfile = open(logFullPath, "a")
for event in xrange(self.lvActions.GetItemCount()):
date = self.lvActions.GetItem(event, 0).GetText() # item in column 0
filename = self.lvActions.GetItem(event, 1).GetText() # col 1, etc
action = self.lvActions.GetItem(event, 2).GetText()
logfile.write( "{0}, {1}, {2}\n".format(date, filename, action)
logfile.close()
GetItem() returns a ListItem object that represents the data in that row/column. I then use the GetText() method to get the text from that item object. You should probably also add error as appropriate. Also, I have used hard coded column names (based on your input). You'll need to adjust those as appropriate.
Upvotes: 1