Python Forum
find an replace not in all entries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find an replace not in all entries
#1
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]
Larz60+ write Mar-01-2021, 11:16 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Working with Excel and Word, Several Questions Regarding Find and Replace Brandon_Pickert 4 1,570 Feb-11-2023, 03:59 PM
Last Post: Brandon_Pickert
  Find and Replace numbers in String giddyhead 2 1,244 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Find and replace in files with regex and Python Melcu54 0 1,852 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  Find and replace to capitalize with Regex hermobot 2 2,526 Mar-21-2020, 12:30 PM
Last Post: hermobot
  Search & Replace - Newlines Added After Replace dj99 3 3,406 Jul-22-2018, 01:42 PM
Last Post: buran
  How to replace entries in a list? Alberto 1 2,990 Jul-17-2017, 06:34 PM
Last Post: ichabod801
  Matching whole words in find/replace script greektranslator 4 13,366 Jul-11-2017, 12:37 PM
Last Post: greektranslator

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020