Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
replace text in a txt
#18
import re

def is_condition_met(line):
    # Utilizza un'espressione regolare per estrarre la percentuale e il valore tra parentesi
    match = re.search(r'(\d+\.\d+%)\s+\((\d+\.\d+)\)', line)
    
    if match:
        percentuale = float(match.group(1)[:-1])  # Rimuove il simbolo % e converte in float
        valore = float(match.group(2))
        
        # Verifica le condizioni richieste
        if percentuale > 75 and valore > 1.2:
            return True

    return False

with open('statistics.txt', 'r') as file:
    lines = file.readlines()

    for line in lines:
        if is_condition_met(line):
            print(line.strip())  # Stampa la linea senza spazi iniziali/finali
can be done something in python to have a "window" to choice which field to choice then do the search?
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,896 Oct-17-2023, 06:03 PM
Last Post: Devan
  python-docx regex: replace any word in docx text Tmagpy 4 2,338 Jun-18-2022, 09:12 AM
Last Post: Tmagpy
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,296 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,120 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Search & Replace - Newlines Added After Replace dj99 3 3,466 Jul-22-2018, 01:42 PM
Last Post: buran
  Need to replace (remove) Unicode characters in text ineuw 1 8,663 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