Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace text in a txt
#5
One way is to define required replacements in dictionary, then iterate over rows in file, split row to words and replace if needed or keep and write to new file. I haven't tested it but something like this should work (this takes advantage of dict.get method where it is possible to provide default value when key is not found):

replacements = {"Verona": "Hellas Verona",
                "Sassuolu": "Sassuolu Calcio",
                "Monza": "AC Monza",
                "11.11%": "",
                "-1.11%": ""}

with open("source.txt", "r") as source, open("output.txt", "w") as target:
    for line in source:
        print(" ".join(replacements.get(word, word) for word in line.split()), file=target)
cartonics likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
replace text in a txt - by cartonics - Oct-12-2023, 07:50 AM
RE: replace text in a txt - by buran - Oct-12-2023, 07:52 AM
RE: replace text in a txt - by cartonics - Oct-12-2023, 08:01 AM
RE: replace text in a txt - by buran - Oct-12-2023, 09:11 AM
RE: replace text in a txt - by perfringo - Oct-12-2023, 09:43 AM
RE: replace text in a txt - by cartonics - Oct-12-2023, 10:54 AM
RE: replace text in a txt - by deanhystad - Oct-12-2023, 08:01 PM
RE: replace text in a txt - by cartonics - Oct-13-2023, 07:31 AM
RE: replace text in a txt - by deanhystad - Oct-13-2023, 11:35 AM
RE: replace text in a txt - by cartonics - Oct-13-2023, 12:48 PM
RE: replace text in a txt - by deanhystad - Oct-13-2023, 01:27 PM
RE: replace text in a txt - by cartonics - Oct-13-2023, 02:28 PM
RE: replace text in a txt - by deanhystad - Oct-13-2023, 06:35 PM
RE: replace text in a txt - by cartonics - Oct-13-2023, 08:01 PM
RE: replace text in a txt - by deanhystad - Oct-13-2023, 08:42 PM
RE: replace text in a txt - by cartonics - Oct-14-2023, 06:33 AM
RE: replace text in a txt - by nicoali - Dec-15-2023, 06:28 PM
RE: replace text in a txt - by nicoali - Dec-16-2023, 04:07 PM
RE: replace text in a txt - by logicielfr - Jan-23-2024, 09:33 PM
RE: replace text in a txt - by Athi - Jan-30-2024, 06:58 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 4,516 Oct-17-2023, 06:03 PM
Last Post: Devan
  python-docx regex: replace any word in docx text Tmagpy 4 2,443 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,553 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 6,345 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Search & Replace - Newlines Added After Replace dj99 3 3,535 Jul-22-2018, 01:42 PM
Last Post: buran
  Need to replace (remove) Unicode characters in text ineuw 1 8,766 Jan-02-2018, 08:01 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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