0

I have an app which loads the contents of a .txt file into a multiline textctrl, I then want to do a 'for loop' on this textctrl and get each line of text and put it into an array for further manipulation.

can it be done like this? or would it be better to open the file, read the text into an array and then display it into a textctrl?

and if so how would I put the text from a file straight into an array?

def OnOpen(self, e):
    dlg = wx.FileDialog(self, "Choose a file to open", self.dirname, "", "*.txt", wx.OPEN) #open the dialog boxto open file
    if dlg.ShowModal() == wx.ID_OK:  #if positive button selected....
        directory, filename = dlg.GetDirectory(), dlg.GetFilename()
        self.filePath = '/'.join((directory, filename))     #get the directory and filename of where file is located
        directory, filename = dlg.GetDirectory(), dlg.GetFilename()
        #self.urlFld.LoadFile(self.filePath)
        self.fileTxt.SetValue(self.filePath)
jerrythebum
  • 330
  • 1
  • 6
  • 17

1 Answers1

1

I would just create an empty list and append the lines from the file to it:

myList = []
for line in myOpenFileObj:
    myList.append(line)

Then add the text to the text control as you were already doing.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • i have my array, but it only prints the last line of text. i have 3 lines of text in a .txt document, i open this into an array as you mentioned above and split this at the "\n" then write it into the textctrl, yet only the last line appears in the textctrl. – jerrythebum Apr 15 '13 at 19:28
  • def checkBtnClick(self, e): urlList = self.myList for i in (urlList): for j in i.split("\n"): self.urlFld.SetValue(j) – jerrythebum Apr 15 '13 at 19:29
  • SetValue just overwrites the entire value in the control. Try AppendText instead: http://wxpython.org/docs/api/wx.TextCtrl-class.html – Mike Driscoll Apr 15 '13 at 21:01