wxpython从.csv文件

时间:2016-03-04 23:53:45

标签: csv wxpython listctrl

我想用来自csv文件的数据填充ListCtrl。我在ListCtrl上显示所有数据时遇到问题,但我可以使用print命令返回各个值。现在,我得到的是在最后一行之前显示的第一列数据。希望有人能帮助我修复下面的破解例子。

import wx
import csv

MYCVS_FILE = "TestData.csv"

class wxFrame (wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__ (self, parent, id = wx.ID_ANY, title = "My Application", size = wx.Size(800,300), style = wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP)

        self.panel = wxPanel(self)

class wxPanel (wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__ (self, parent, id = wx.ID_ANY, size = wx.Size(760,300))

        self.button1 = wx.Button(self, wx.ID_ANY, u"Read_CSV")
        self.button1.Bind(wx.EVT_LEFT_DOWN, self.read_CSV)
        self.button2 = wx.Button(self, wx.ID_ANY, u"Write_CSV")
        self.button2.Bind(wx.EVT_LEFT_DOWN, self.write_CSV)

        self.listctrl = wx.ListCtrl(self, wx.ID_ANY, wx.DefaultPosition, wx.Size(760,200), wx.LC_HRULES | wx.LC_REPORT | wx.SIMPLE_BORDER | wx.VSCROLL | wx.LC_SORT_ASCENDING)
        self.listctrl.InsertColumn(0, u"Date", width=100)
        self.listctrl.InsertColumn(1, u"Time", width=100)
        self.listctrl.InsertColumn(2, u"Level", width=100)
        self.listctrl.InsertColumn(3, u"Description", width=460)

        vsizer = wx.BoxSizer(wx.VERTICAL)
        hsizer1 = wx.BoxSizer(wx.HORIZONTAL)
        hsizer2 = wx.BoxSizer(wx.HORIZONTAL)

        hsizer1.Add(self.listctrl, 0, wx.TOP | wx.BOTTOM | wx.ALIGN_CENTER_VERTICAL, 10)
        vsizer.Add(hsizer1, 0, wx.ALIGN_CENTER_HORIZONTAL)

        hsizer2.Add(self.button1, 0, wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)
        hsizer2.Add(self.button2, 0, wx.LEFT | wx.RIGHT | wx.ALIGN_CENTER_VERTICAL, 5)

        vsizer.Add(hsizer2, 0, wx.ALIGN_CENTER_HORIZONTAL)

        self.SetSizer(vsizer)
        self.Layout()

    def read_CSV(self, event):

        self.listctrl.DeleteAllItems()

        with open(MYCVS_FILE, "rb") as csv_file:
            reader = csv.reader(csv_file, delimiter=",", quoting=csv.QUOTE_NONE)
            for row in reader:
                self.listctrl.Append(row)
            print "Read complete"

    def write_CSV(self, event):

        sample_data = ["04-Mar-2016,04:05:08,INFO,Event log created".split(","),
                       "04-Mar-2016,04:05:14,CRITICAL,Image file is missing".split(","),
                       "04-Mar-2016,17:10:54,INFO,This is another test...1".split(","),
                       "04-Mar-2016,18:11:55,INFO,This is another test...2".split(","),
                       "04-Mar-2016,19:12:56,INFO,This is another test...3".split(",")]

        with open(MYCVS_FILE, "wb") as csv_file:
            writer = csv.writer(csv_file, delimiter=",")
            for row in sample_data:
                writer.writerow(row)
            print "Write complete"



if __name__ == "__main__":

    app = wx.App(False)
    frame = wxFrame(None)
    frame.Show()
    app.MainLoop()

Current result of reading csv file

0 个答案:

没有答案