Frage

I'm trying to use a multiline TextCtrl to get user inputted multiline CSV data and convert it to a 2D array. I've not been successful. Any ideas? Thanks in advance.

import wx
import csv
import StringIO
Title="MultiLine TextCtrl to 2D Array"

class MainFrame(wx.Frame):
    def __init__(self,title):
        wx.Frame.__init__(self, None, title=title, pos=(140,140), size=(320,300))
        panel=Panel(self)

class Panel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.InputData= wx.TextCtrl(self, value="1,2,3,4", pos=(20, 20), size=(150,200), style=wx.TE_MULTILINE)
        self.button =wx.Button(self, label="GO", pos=(200,200))
        self.Bind(wx.EVT_BUTTON, self.OnClick,self.button)
    def OnClick(self,event):      
        DataString=self.InputData.GetValue()
        f=StringIO.StringIO(DataString)
        reader = csv.reader(f, delimiter=',')
        x=list(reader)
        print x
        print x[0,0]

if __name__=="__main__":
    app = wx.App(redirect=False)
    frame = MainFrame(Title)
    frame.Show()
    app.MainLoop()

I would use wxGrid, but I want to be able to paste CSV text into the field, and I don't know of a way to do that with wxGrid. Here is a sample of the data I want to be able to paste into the field:

Point,X,Y,Z
1,-.500,-15.531,.000
2,.000,-15.531,2.354
3,.000,-14.719,2.354
4,.000,-14.719,2.273
5,.000,-14.531,2.273
War es hilfreich?

Lösung

csv.reader returns a list of lists, so you would print out an element using a term like x[3][2], where 3 would select the row, and 2 would select the column.

Indexing likex[3,2] does not work for a list of list, but is more for multidimensional arrays, like would be used with numpy.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top