【问题标题】:wxPython: How to exempt (let off) empty CSV columns and rows from importing into wxGridwxPython:如何免除(放开)空的 CSV 列和行导入 wxGrid
【发布时间】:2014-06-21 06:28:27
【问题描述】:

我有一个将 CSV 文件导入 wxGrid 的脚本,现在因为用户可以导入包含空列和行的 CSV 文件数据。我想排除所有完全空的列和行被导入到网格中。 当 CSV 导入 wxGrid 时,我希望删除完全空的列

我不知道该怎么做,任何帮助将不胜感激。

csv1.py 是 GUI 脚本

import wx


###########################################################################
## Class MyFrame3
###########################################################################

class MyFrame3 ( wx.Frame ):

    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 900,600 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        Sizer1 = wx.BoxSizer( wx.HORIZONTAL )

        Sizer1.SetMinSize( wx.Size( 0,0 ) ) 
        self.Right_Panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        RightSizer = wx.BoxSizer( wx.VERTICAL )


        self.Right_Panel.SetSizer( RightSizer )
        self.Right_Panel.Layout()
        RightSizer.Fit( self.Right_Panel )
        Sizer1.Add( self.Right_Panel, 1, wx.EXPAND |wx.ALL, 5 )

        self.Left_Panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        LeftSizer = wx.BoxSizer( wx.VERTICAL )

        self.ImportButton = wx.Button( self.Left_Panel, wx.ID_ANY, u"Import CSV File", wx.DefaultPosition, wx.DefaultSize, 0 )
        LeftSizer.Add( self.ImportButton, 0, wx.ALL, 5 )


        self.Left_Panel.SetSizer( LeftSizer )
        self.Left_Panel.Layout()
        LeftSizer.Fit( self.Left_Panel )
        Sizer1.Add( self.Left_Panel, 0, wx.EXPAND |wx.ALL, 5 )


        self.SetSizer( Sizer1 )
        self.Layout()
        self.menubar = wx.MenuBar( 0 )
        self.fileMenu = wx.Menu()
        self.importMenu = wx.MenuItem( self.fileMenu, wx.ID_ANY, u"Import", wx.EmptyString, wx.ITEM_NORMAL )
        self.fileMenu.AppendItem( self.importMenu )

        self.menubar.Append( self.fileMenu, u"&File" ) 

        self.SetMenuBar( self.menubar )


        self.Centre( wx.BOTH )

        # Connect Events
        self.ImportButton.Bind( wx.EVT_BUTTON, self.ImportFunc )
        self.Bind( wx.EVT_MENU, self.ImportFunc, id = self.importMenu.GetId() )

csv2.py是运行脚本

#!/usr/bin/python
# -*- coding: utf-8 -*- 

import wx
import os
import sys, csv
import wx.grid
from csv1 import MyFrame3

class MyFrame(MyFrame3):
    def __init__(self, parent, size = wx.Size(900,600)):
        MyFrame3.__init__ (self, parent)

        self.dirname = os.getcwd()



    # Import/Open CSV

    def ImportFunc( self, event ):

        dlg=wx.FileDialog(self, 'Choose a file', self.dirname, '','CSV files (*.csv)|*.csv',wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.dirname=dlg.GetDirectory()
            self.filename=os.path.join(self.dirname,dlg.GetFilename())
            self.file=file(self.filename, 'r')

            #check for file format with sniffer
            dialect = csv.Sniffer().sniff(self.file.read(1024))
            self.file.seek(0)

            csvfile=csv.reader(self.file,dialect)
            filedata = [] #put contents of csvfile into a list
            filedata.extend(csvfile)
            self.file.seek(0)

            #grab a sample and see if there is a header
            sample=self.file.read(2048)
            self.file.seek(0)
            if csv.Sniffer().has_header(sample): #if there is a header
                colnames=csvfile.next() # label columns from first line
                datalist=[] # create a list without the header
                datalist.extend(filedata[1:len(filedata)]) #append data without header

            else:
                row1=csvfile.next() #if there is NO header
                colnames=[]
                for i in range(len(row1)):
                    colnames.append('col_%d' % i) # label columns as col_1, col_2, etc
                self.file.seek(0)
                datalist=filedata #append data to datalist

            self.file.close()
            self.createGrid(datalist, colnames)


            grid_sizer = wx.BoxSizer(wx.VERTICAL)
            grid_sizer.Add(self.grid, 1, wx.EXPAND)
            self.Right_Panel.SetSizer(grid_sizer)
            self.Right_Panel.Layout()



    #create the grid

    def createGrid(self, datalist, colnames):
        if getattr(self, 'grid', 0): self.grid.Destroy()
        self.grid=wx.grid.Grid(self.Right_Panel, 0)
        self.grid.CreateGrid(len(datalist), len(colnames)) #create grid, same size as file (rows, cols)

        #fill in headings
        for i in range(len(colnames)):
            self.grid.SetColLabelValue(i, colnames[i])

        #populate the grid
        for row in range(len(datalist)):
            for col in range(len(colnames)):
                try: 
                    self.grid.SetCellValue(row,col,datalist[row][col])
                except: 
                    pass


        self.grid.AutoSizeColumns(False) # size columns to data (from cvsomatic.py)
        self.twiddle()

    def twiddle(self): # from http://www.velocityreviews.com/forums/t330788-how-to-update-window-after-wxgrid-is-updated.html
        x,y = self.GetSize()
        self.SetSize((x, y+1))
        self.SetSize((x,y))

    def Exit(self, event):
        if getattr(self, 'file',0):
            self.file.close()
            self.Close(True)

# import wx.lib.mixins.inspection
app = wx.App(0)
Frame_02 = MyFrame(None)
Frame_02.Show()
# wx.lib.inspection.InspectionTool().Show()
app.MainLoop()

提前感谢您的宝贵时间。

【问题讨论】:

    标签: python csv wxpython wxwidgets


    【解决方案1】:

    这根本不是 wxPython 问题。您在 wxGrid 中显示 CSV 数据的事实只是实现。您真正想要的是从您从 CSV 生成的列表中过滤掉空行/列。下面的想法可以让你开始(但根本没有优化 - 我想展示你应该采取的个别基本步骤,作为修改原始代码的想法):

    # remove the empty row(s):
    filedata = [row for row in filedata if not row.strip() and any(field.strip() for field in row)]
    
    # remove the empty col(s):
    not_empty_cols = set()
    colcount = 0
    for row in filedata:
        if colcount > 0 and len(colcount) == len(not_empty_cols):
            # no need to continue, there aren't any empty cols
            break
        if len(row) > 0:
            colcount = len(row)
        for idx, field in enumerate(row):
            if idx in not_empty_cols:
                continue
            if field.strip():
                not_empty_cols.add(idx)
    all_cols = set(range(colcount))
    empty_cols = sorted(list(all_cols.difference(not_empty_cols)))
    for row in filedata:
        for col in empty_cols:
            row.pop(col)
    

    抱歉,我没有测试上述内容。如果它不起作用,请将其视为伪代码。 :)

    关于 wxGrid,尤其是当您的数据变大时,您应该研究创建虚拟网格(只跟踪当前屏幕上的项目,而不是整个数据结构的网格)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-29
      • 2021-03-07
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      • 2019-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多