0

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
Mike
  • 1,561
  • 3
  • 15
  • 18
  • How about using a wxGrid or similar structure? – tom10 Jun 04 '13 at 16:32
  • Thanks for the suggestion, but I want to be able to copy and paste CSV text into the field. I don't know a way of doing this with wxGrid, is there one? – Mike Jun 04 '13 at 16:35
  • How about intercepting the Paste command and then parse the csv data and enter it into the table? (I don't mean to push this idea, and I have no idea what you mean by "not been successful", so take it or leave it, but either way your treating the data as columns, so you might as well use the tool that's meant for that.) – tom10 Jun 04 '13 at 18:45
  • If that works I'm all for it, but I have no idea how to do that. I'm just trying to take text that can be copied from one window and turn it into an array that I can do math on in Python, but I haven't been successful in getting it into a 2D array. – Mike Jun 04 '13 at 20:10
  • 1
    OK, but so I don't solve a problem you're not interested in, can you describe better really what you want to do? At one point you say "paste into the field", but I don't see where you try to do that in the code, yet your OnClick code just has a simple problem that you need to say x[0][0] instead of x[0,0]. Is that last point all you needed? (When I run your example, I can paste the text into the text ctrl, it's just not formatted.) – tom10 Jun 04 '13 at 20:58
  • Actually, I think that's all I needed! I was manually pasting the text into the field, but when I tried to read it, I guess the x[0,0] was inappropriate. I didn't realize I could do x[0][0], but it looks like that's the trick. Thanks! – Mike Jun 04 '13 at 21:45
  • OK, I put it as an answer then. Glad it worked out. – tom10 Jun 05 '13 at 02:59

1 Answers1

1

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.

tom10
  • 67,082
  • 10
  • 127
  • 137