Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace text in a txt
#1
# Read in the file
with open('lista.txt', 'r') as file:
  filedata = file.read()

# Replace the target string
filedata = filedata.replace('Verona', 'Hellas Verona')

# Write the file out again
with open('fileok.txt', 'w') as file:
  file.write(filedata)
i have a txt with some text in this structure


Output:
Verona - Napoli 10.42% (9.60) 1.11% 1 27.08% (3.69) 11.11% X 62.5% (1.60) -11.11% 2 37.5% (2.67) 11.11% 1X 89.58% (1.12) -1.11% X2 72.92% (1.37) -11.11% 12 Torino - Inter 15.38% (6.50) 1.11% 1 23.08% (4.33) 1.1% X 61.54% (1.62) -11.11% 2 38.46% (2.60) 11.11% 1X 84.62% (1.18) -1.11% X2 76.92% (1.30) -1.1% 12 Sassuolo - Lazio 24.07% (4.15) 11.11% 1 29.63% (3.37) -11.11% X 46.3% (2.16) 1.11% 2 53.7% (1.86) -1.11% 1X 75.93% (1.32) -11.11% X2 70.37% (1.42) 11.11% 12 AS Roma - Monza 57.58% (1.74) 1 33.33% (3.00) X 9.09% (11.00) 2 90.91% (1.10) 1X 42.42% (2.36) X2 66.67% (1.50) 12 Bologna - Frosinone 52.38% (1.91) -1.11% 1 29.37% (3.40) 1.1% X 18.25% (5.48) 1.11% 2 81.75% (1.22) -1.11% 1X 47.62% (2.10) 1.11% X2 70.63% (1.42) -1.1% 12 Salernitana - Cagliari 37.78% (2.65) 11.11% 1 28.89% (3.46) -1.11% X 33.33% (3.00) -1.11% 2 66.67% (1.50) 1.11% 1X 62.22% (1.61) -11.11% X2 71.11% (1.41) 1.11% 12 Atalanta - Genoa 72.3% (1.38) 11.11% 1 17.57% (5.69) -11.11% X 10.14% (9.86) 1.11% 2 89.86% (1.11) -1.11% 1X 27.7% (3.61) -11.11% X2 82.43% (1.21) 11.11% 12 AC Milan - Juventus 43.35% (2.31) -11.1% 1 30.64% (3.26) 11.11% X 26.01% (3.84) -1.11% 2 73.99% (1.35) 1.11% 1X 56.65% (1.77) 11.1% X2 69.36% (1.44) -11.11% 12 Udinese - Lecce 44.1% (2.27) 11.11% 1 32.75% (3.05) -11.1% X 23.14% (4.32) 1.11% 2 76.86% (1.30) -1.11% 1X 55.9% (1.79) -11.11% X2 67.25% (1.49) 11.1% 12 Fiorentina - Empoli 68.59% (1.46) -11.11% 1 19.87% (5.03) 11.11% X 11.54% (8.67) 1.11% 2 88.46% (1.13) -1.11% 1X 31.41% (3.18) 11.11% X2 80.13% (1.25) -11.11% 12 Genoa - Salernitana 54.55% (1.83) 11.11% 1 25.97% (3.85) -11.11% X 19.48% (5.13) 11.11% 2 80.52% (1.24) -11.11% 1X 45.45% (2.20) -11.11% X2 74.03% (1.35) 11.11% 12 Sassuolo - Bologna 45.39% (2.20) -1.11% 1 26.95% (3.71) 1.11% X 27.66% (3.62) -1.1% 2 72.34% (1.38) 1.1% 1X 54.61% (1.83) 1.11% X2 73.05% (1.37) -1.11% 12 Lecce - Torino 33.96% (2.94) 11.11% 1 26.42% (3.79) -1.11% X 39.62% (2.52) -1.11% 2 60.38% (1.66) 1.11% 1X 66.04% (1.51) -11.11% X2 73.58% (1.36) 1.11% 12 Juventus - Verona 67.94% (1.47) 1.1% 1 22.14% (4.52) -1.11% X 9.92% (10.08) 1.11% 2 90.08% (1.11) -1.11% 1X 32.06% (3.12) -1.1% X2 77.86% (1.28) 1.11% 12 Cagliari - Frosinone 28.57% (3.50) 1 23.81% (4.20) X 47.62% (2.10) 2 52.38% (1.91) 1X 71.43% (1.40) X2 76.19% (1.31) 12 Monza - Udinese 37.04% (2.70) 11.11% 1 35.8% (2.79) 11.11% X 27.16% (3.68) -11.11% 2 72.84% (1.37) 11.11% 1X 62.96% (1.59) -11.11% X2 64.2% (1.56) -11.11% 12 Inter - AS Roma 55.1% (1.81) 1.11% 1 26.53% (3.77) -1.11% X 18.37% (5.44) -1.1% 2 81.63% (1.23) 1.1% 1X 44.9% (2.23) -1.11% X2 73.47% (1.36) 1.11% 12 Napoli - AC Milan 38.57% (2.59) 11.1% 1 40% (2.50) -11.11% X 21.43% (4.67) 1.11% 2 78.57% (1.27) -1.11% 1X 61.43% (1.63) -11.1% X2 60% (1.67) 11.11% 12 Empoli - Atalanta 25% (4.00) 1 37.5% (2.67) X 37.5% (2.67) 2 62.5% (1.60) 1X 75% (1.33) X2 62.5% (1.60) 12 Lazio - Fiorentina 41% (2.44) 1.11% 1 26.5% (3.77) -11.11% X 32.5% (3.08) 11.11% 2 67.5% (1.48) -11.11% 1X 59% (1.69) -1.11% X2 73.5% (1.36) 11.11% 12 Verona - Napoli 10.42% (9.60) 1.11% 1 27.08% (3.69) 11.11% X 62.5% (1.60) -11.11% 2 37.5% (2.67) 11.11% 1X 89.58% (1.12) -1.11% X2 72.92% (1.37) -11.11% 12 Torino - Inter 15.38% (6.50) 1.11% 1 23.08% (4.33) 1.1% X 61.54% (1.62) -11.11% 2 38.46% (2.60) 11.11% 1X 84.62% (1.18) -1.11% X2 76.92% (1.30) -1.1% 12 Sassuolo - Lazio 24.07% (4.15) 11.11% 1 29.63% (3.37) -11.11% X 46.3% (2.16) 1.11% 2 53.7% (1.86) -1.11% 1X 75.93% (1.32) -11.11% X2 70.37% (1.42) 11.11% 12 AS Roma - Monza 57.58% (1.74) 1 33.33% (3.00) X 9.09% (11.00) 2 90.91% (1.10) 1X 42.42% (2.36) X2 66.67% (1.50) 12 Bologna - Frosinone 52.38% (1.91) -1.11% 1 29.37% (3.40) 1.1% X 18.25% (5.48) 1.11% 2 81.75% (1.22) -1.11% 1X 47.62% (2.10) 1.11% X2 70.63% (1.42) -1.1% 12 Salernitana - Cagliari 37.78% (2.65) 11.11% 1 28.89% (3.46) -1.11% X 33.33% (3.00) -1.11% 2 66.67% (1.50) 1.11% 1X 62.22% (1.61) -11.11% X2 71.11% (1.41) 1.11% 12 Atalanta - Genoa 72.3% (1.38) 11.11% 1 17.57% (5.69) -11.11% X 10.14% (9.86) 1.11% 2 89.86% (1.11) -1.11% 1X 27.7% (3.61) -11.11% X2 82.43% (1.21) 11.11% 12 AC Milan - Juventus 43.35% (2.31) -11.1% 1 30.64% (3.26) 11.11% X 26.01% (3.84) -1.11% 2 73.99% (1.35) 1.11% 1X 56.65% (1.77) 11.1% X2 69.36% (1.44) -11.11% 12 Udinese - Lecce 44.1% (2.27) 11.11% 1 32.75% (3.05) -11.1% X 23.14% (4.32) 1.11% 2 76.86% (1.30) -1.11% 1X 55.9% (1.79) -11.11% X2 67.25% (1.49) 11.1% 12 Fiorentina - Empoli 68.59% (1.46) -11.11% 1 19.87% (5.03) 11.11% X 11.54% (8.67) 1.11% 2 88.46% (1.13) -1.11% 1X 31.41% (3.18) 11.11% X2 80.13% (1.25) -11.11% 12 Genoa - Salernitana 54.55% (1.83) 11.11% 1 25.97% (3.85) -11.11% X 19.48% (5.13) 11.11% 2 80.52% (1.24) -11.11% 1X 45.45% (2.20) -11.11% X2 74.03% (1.35) 11.11% 12 Sassuolo - Bologna 45.39% (2.20) -1.11% 1 26.95% (3.71) 1.11% X 27.66% (3.62) -1.1% 2 72.34% (1.38) 1.1% 1X 54.61% (1.83) 1.11% X2 73.05% (1.37) -1.11% 12 Lecce - Torino 33.96% (2.94) 11.11% 1 26.42% (3.79) -1.11% X 39.62% (2.52) -1.11% 2 60.38% (1.66) 1.11% 1X 66.04% (1.51) -11.11% X2 73.58% (1.36) 1.11% 12 Juventus - Verona 67.94% (1.47) 1.1% 1 22.14% (4.52) -1.11% X 9.92% (10.08) 1.11% 2 90.08% (1.11) -1.11% 1X 32.06% (3.12) -1.1% X2 77.86% (1.28) 1.11% 12 Cagliari - Frosinone 28.57% (3.50) 1 23.81% (4.20) X 47.62% (2.10) 2 52.38% (1.91) 1X 71.43% (1.40) X2 76.19% (1.31) 12 Monza - Udinese 37.04% (2.70) 11.11% 1 35.8% (2.79) 11.11% X 27.16% (3.68) -11.11% 2 72.84% (1.37) 11.11% 1X 62.96% (1.59) -11.11% X2 64.2% (1.56) -11.11% 12 Inter - AS Roma 55.1% (1.81) 1.11% 1 26.53% (3.77) -1.11% X 18.37% (5.44) -1.1% 2 81.63% (1.23) 1.1% 1X 44.9% (2.23) -1.11% X2 73.47% (1.36) 1.11% 12 Napoli - AC Milan 38.57% (2.59) 11.1% 1 40% (2.50) -11.11% X 21.43% (4.67) 1.11% 2 78.57% (1.27) -1.11% 1X 61.43% (1.63) -11.1% X2 60% (1.67) 11.11% 12 Empoli - Atalanta 25% (4.00) 1 37.5% (2.67) X 37.5% (2.67) 2 62.5% (1.60) 1X 75% (1.33) X2 62.5% (1.60) 12 Lazio - Fiorentina 41% (2.44) 1.11% 1 26.5% (3.77) -11.11% X 32.5% (3.08) 11.11% 2 67.5% (1.48) -11.11% 1X 59% (1.69) -1.11% X2 73.5% (1.36) 11.11% 12
is it possible to replace all text inside it? and not for only one?

Verona -> Hellas Verona
Sassuolo -> Sassuolo Calcio
Monza -> Ac Monza
and remove all 11.11% and -1.11%
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 3,667 Oct-17-2023, 06:03 PM
Last Post: Devan
  python-docx regex: replace any word in docx text Tmagpy 4 2,299 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,243 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,009 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Search & Replace - Newlines Added After Replace dj99 3 3,445 Jul-22-2018, 01:42 PM
Last Post: buran
  Need to replace (remove) Unicode characters in text ineuw 1 8,641 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