Python Forum

Full Version: find an replace not in all entries
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello, i am new in python. I try to make a programm wich translate russian pattern in german pattern. It works but not in all words. Why?

f1 = open('wolf.txt', 'r')
f2 = open('out.txt.tmp', 'w')
for line in f1:
        f2.write(line.replace('ряд:', 'Runde:'))
        f2.write(line.replace('сбн,','fM,'))
        f2.write(line.replace('повторить','*'))
        f2.write(line.replace('раз',' '))
        f2.write(line.replace('[12сбн]','[12]')) 
f1.close()
f2.close()
[Image: out-txt-tmp.jpg]
Instead of writing the line out to the file with each replace, I think you wand to do all the replacements to the line and the write it to the out file. Try this:

f1 = open('wolf.txt', 'r')
f2 = open('out.txt.tmp', 'w')
for line in f1:
        line = line.replace('ряд:', 'Runde:')
        line = line.replace('сбн,','fM,')
        line = line.replace('повторить','*')
        line = line.replace('раз',' ')
        line = line.replace('[12сбн]','[12]') 
        f2.write(line) 
f1.close()
f2.close()