Update a text file with ( new words+ \n ) after the words is appended into a list

71 views Asked by At

I have this problem and I am looking forward to see your positive answers and solutions. the codes bellow: when the program detects misspelled words after the text split it append each misspelled word into a list [a] and now my problem is that how can I write(update) to dictionary with the misspelled words in a way that each line in the dictionary contains only one word. so far I have tried and it only writes the current insert and deletes the previous records. any help is appreciated ( THANKS)

  def onaddbutton(self,event):
    os.chdir('d:/KKSC')
    dic = getDic()
    print dic[0], dic[1], dic[2]
    text = tokenize_editor_text(self.controlz.GetValue())     
    a = []
    for word in text:
        if word not in dic:
            misspelled = word
            a.append(misspelled)
            f= (misspelled)
            with open('test.text', encoding='utf-8', mode='w') as f:
            f.write(misspelled)
            f.write('\n')
            f.seek(0)
            print(repr(f.readline()[:1]))
2

There are 2 answers

1
Jonathan On

Please update your indentation.

you're problem is you're opening the file with mode="w". Try mode="a" for append instead.

Also, seek(0) will move the current position in the file to the first line, first column. That isn't what you seem to want. Get rid of that line.

0
The Tasty Jaffa On

Here is a list of file access modes:

"r" - Read from a text file, if it doesn't exist then python will complain

"w" - Write to a text file. If it exists, it's contents are overwriten. If not it' created

"a" - Append to a text file, new data is appended to it. if it doesn't exist, it's created

"w+" - Write to and read to a text file. Contents overwritten, or created

"r+" - Read from and write to a text file. Complain if cannot find it

"a+" - Append to and read from a text file.

In your code you've writen "open('test.text', encoding='utf-8', mode='w')"

And if you look at my list i made before "w" writes to and overwites it's contents... But to add to it you'll need "a" or "a+" if you want to read it as well.

So to append (add to it's contents) to a text file:

Text_file = open("Text file", "a")

So if we make those changes your program will now look like this:

def onaddbutton(self,event):
       os.chdir('d:/KKSC')
       dic = getDic()
       print dic[0], dic[1], dic[2]
       text = tokenize_editor_text(self.controlz.GetValue())     
       a = []
       for word in text:
           if word not in dic:
               misspelled = word
               a.append(misspelled)
               f= (misspelled)
               with open('test.text', 'utf-8', 'a+') as f:
               f.append(misspelled\n)
        print(repr(f.readline()[:1]))

A few other things with text files...

Text_file.close()               # Closes the text file so you can open it again
Text_file.read()                # Reads all lines in a text file
Text_file.readlines(**X**)      # Reads 'x' number of lines

Also they are always a string... and to tidy up the text file you could do...

Dictionary = Text_file.read() # reads all lines in text file 
Dictionary = sort(Dictionary)
Text_file.close() # Closes the text file.
Text_file = open("Text file.txt", "w") #Opens text file and overwrites what's in it
Text_file.write(Dictionary) # Writes ordered text into file.

Which should order them alphabetical order.