Python Forum
replace text in a txt - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: replace text in a txt (/thread-40909.html)

Pages: 1 2


replace text in a txt - cartonics - Oct-12-2023

# 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%


RE: replace text in a txt - buran - Oct-12-2023

(Oct-12-2023, 07:50 AM)cartonics Wrote: is it possible to replace text inside it?
Yes, it's possible. What have you tried so far and what specific problem do you have?


RE: replace text in a txt - cartonics - Oct-12-2023

i have to add all this lines?

filedata = filedata.replace('Verona', 'Hellas Verona')
filedata = filedata.replace('Sassuolo', 'Sassuolo Calcio')
one fo each? or there is a faster way?


RE: replace text in a txt - buran - Oct-12-2023

you can use a dict that maps old:new values and loop over it, instead or writing multiple repetitive lines
replacements = {'Verona':'Hellas Verona', 'Sassuolo':'Sassuolo Calcio'}

for old_val, new_val in replacements.items():
    fielddata = filedata.replace(old_val, new_val)



RE: replace text in a txt - perfringo - Oct-12-2023

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)



RE: replace text in a txt - cartonics - Oct-12-2023

Thank you so much for the methods and i come with a new question:


i want to build an insert query for each match from that file and then send it to my own database..


for example from

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

create the query
INSERT INTO sbostats (partita,p1,pX,p2,p1x,px2,p12) VALUES ("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 " );

and so on...

i have to take the number of rows of file and then do same from line 1 to 7 than 8 to 14 and so on?

import mysql.connector
from mysql.connector import errorcode



mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  database="my_db"
)

mycursor = mydb.cursor()

sql = "INSERT INTO sbostats (partita,p1,pX,p2,p1x,px2,p12) VALUES (%s, %s,%s, %s,%s, %s, %s)"
val = ("test1", "test2","3", "4", "5", "6", "7")

mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")
i saw also that i can connect to my database :) and i can send the query .. All was fine

so i have to put datas in "val" and iterate.. but i have no idea how to do that!


RE: replace text in a txt - deanhystad - Oct-12-2023

It is easy to read the group the lines. You can even do the string replacement at the same time.
from itertools import batched


names = {
    "Verona": "Hellas Verona",
    "Sassuolu": "Sassuolu Calcio",
    "Monza": "AC Monza",
}

with open("test.txt", "r") as file:
    for values in batched(file, 7):
        values = [value.strip() for value in values]
        a, b = values[0].split(" - ")
        values[0] = f"{names.get(a, a)} - {names.get(b, b)}"
        print(values)
If you aren't using Python 3.12 or newer, you can make your own batched.
from itertools import islice


names = {
    "Verona": "Hellas Verona",
    "Sassuolu": "Sassuolu Calcio",
    "Monza": "AC Monza",
}


def batched(iterable, n):
    "Batch data into tuples of length n. The last batch may be shorter."
    # batched('ABCDEFG', 3) --> ABC DEF G
    if n < 1:
        raise ValueError("n must be at least one")
    it = iter(iterable)
    while batch := tuple(islice(it, n)):
        yield batch


with open("test.txt", "r") as file:
    for values in batched(file, 7):
        values = [value.strip() for value in values]
        a, b = values[0].split(" - ")
        values[0] = f"{names.get(a, a)} - {names.get(b, b)}"
        print(values)
I don't see any value in putting this information in a database. A database needs to be carefully designed to be useful. If you could put this information into a database, what kind of queries would you want to perform? Use the queries to define what tables and columns you'll need in your database, then write the code to load the file into database tables. Don't create the tables first then try to figure out how to write a query.


RE: replace text in a txt - cartonics - Oct-13-2023

Quote:I don't see any value in putting this information in a database. A database needs to be carefully designed to be useful. If you could put this information into a database, what kind of queries would you want to perform? Use the queries to define what tables and columns you'll need in your database, then write the code to load the file into database tables. Don't create the tables first then try to figure out how to write a query.

from this txt file i need only the percentages

Verona - Napoli partita column
10.42% p1 column
27.08% px column
62.5% p2 column
37.5% p1X column
89.58% pX2 column
72.92% p12 column

so i need to create and send
inserts like
sql = "INSERT INTO sbostats (partita,p1,pX,p2,p1x,px2,p12) VALUES (%s, %s,%s, %s,%s, %s, %s)"
val = ( "Verona - Napoli", "10.42%", "27.08%","62.5%", "37.5%", "89.58%", "72.92%")
one for each 7 lines (match) of the txt file
then i will make some joins/query and view on other tables of my database.


RE: replace text in a txt - deanhystad - Oct-13-2023

I still see no value here. How can you look up those percentages in the database? Try writing the query that would retrieve them.


RE: replace text in a txt - cartonics - Oct-13-2023

(Oct-13-2023, 11:35 AM)deanhystad Wrote: I still see no value here. How can you look up those percentages in the database? Try writing the query that would retrieve them.

will be something like this:
"SELECT * from calcio inner join sbostats on sbostats.partita = calcio.partita";

but for now i have to insert data from txt to the database so something like:
INSERT INTO sbostats (partita,p1,pX,p2,p1x,px2,p12) VALUES ( "Verona - Napoli", "10.42%", "27.08%","62.5%", "37.5%", "89.58%", "72.92%")

i can also add that columns in the table calcio via insert update with where clause.. but my problem is to build the insert query from txt to database