Question

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)
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top