Python script writes text to a file but does not add the text it should

I am ashamed to resort to help again, but I am stuck.

I have a Spanish novel (in plain text), and I have a Python script that should put translations for complex words in parentheses using a custom dictionary in another text file.

After a lot of trial and error, I managed to run the script and write the novel in a new text file, as it should have done.

The only problem is that the text in the novel was not entered, i.e. translations were not added to the text. A dictionary is a simple text file that is formatted as follows:

[spanish word] [english translation]                                      
[spanish word] [english translation]

etc. Please note that words are not enclosed in brackets. There is one space between each word, and there is no place in it elsewhere.

Here's the abusive code:

bookin = (open("novel.txt")).read()
subin = open("dictionary.txt")
for line in subin.readlines():
    ogword, meaning = line.split(" ")
    subword = ogword + "(meaning)"
    bookin.replace(ogword, subword)
    ogword = ogword.capitalize()
    subword = ogword + "(meaning)"
    bookin.replace(ogword, subword)
subin.close()
bookout = open("output.txt", "w")
bookout.write(bookin)
bookout.close()

.

: MemoryError , , , , . , !

+3
4

:

bookin.replace(ogword, subword)

bookin = bookin.replace(ogword, subword)

: replace - , - .

+7

@David Robinson, , .

 bookin = bookin.replace(ogwrd, subword)

, ( - ), . , - , , , , .

, , 10 / , . . , , .

- , , , , , . , bookin .

bookin = (open("novel.txt")).read()
subin = open("dictionary.txt")

print 'bookin =', bookin  # verify that you read the information 

for line in subin.readlines():
    print 'line = ', line # verify line read

    ogword, meaning = line.split(" ")
    print 'ogword, meaning = ', ogword, meaning # verify ...

    subword = ogword + "(meaning)"
    print 'subword =', subword # verify ...

    bookin.replace(ogword, subword)
    print 'bookin post replace =', bookin # verify ... etc

    ogword = ogword.capitalize()
    subword = ogword + "(meaning)"
    bookin.replace(ogword, subword)

subin.close() 

print 'bookout', bookout # make sure final output is good ...
bookout = open("output.txt", "w")
bookout.write(bookin)
bookout.close()

, , Python , , . , , - ( - ). , , ( ),

 s = 'this is a test'
 print s
 s.replace('this', 'that')
 print s

, s , s = s.replace('this', 'that').

, . . .. .

PS: SO, , .

+2

You can get this information by entering them in the interpreter:

>>> help(str.replace)  
>>> help('a'.replace)  
>>> s = 'a'  
>>> help(s.replace)  
>>> import string  
>>> help(string.replace)
+1
source

Besides the MemoryError, which is amazing considering the size of your files, you still have a few things you can improve on; see comments below:

bookin = open("novel.txt").read() # don't need extra ()
subin = open("dictionary.txt")
# for line in subin.readlines():
# readlines() reads the whole file, you don't need that
for line in subin:
    # ogword, meaning = line.split(" ")
    # the above will leave a newline on the end of "meaning"
    ogword, meaning = line.split()
    # subword = ogword + "(meaning)"
    # if ogword is "gato" and meaning is "cat",
    # you want "gato (cat)"
    # but you will get "gato(meaning)"
    subword = ogword + " (" + meaning + ")"
    bookin = bookin.replace(ogword, subword)
    ogword = ogword.capitalize()
    subword = ogword + "(meaning)"  # fix this also
    bookin.replace(ogword, subword) # fix this also
    print len(bookin) # help debug your MemoryError
subin.close()
bookout = open("output.txt", "w")
bookout.write(bookin)
bookout.close()

You should follow @Levon's recommendations and try your code in some small test data files so you can see what is happening.

After using this one-line dictionary:

gato cat

with this one line romance:

El gato se sirvió un poco de Gatorade para el "alligator".

You can review your high-level strategy.

+1
source

All Articles