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
#2
(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?
cartonics likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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?
Reply
#4
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)
cartonics likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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
#6
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!
Reply
#7
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.
cartonics likes this post
Reply
#8
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.
Reply
#9
I still see no value here. How can you look up those percentages in the database? Try writing the query that would retrieve them.
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 3,467 Oct-17-2023, 06:03 PM
Last Post: Devan
  python-docx regex: replace any word in docx text Tmagpy 4 2,250 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,168 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 5,879 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Search & Replace - Newlines Added After Replace dj99 3 3,413 Jul-22-2018, 01:42 PM
Last Post: buran
  Need to replace (remove) Unicode characters in text ineuw 1 8,606 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