Python Forum
If I need to do regex replacement 27000 times, how to shorten it?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If I need to do regex replacement 27000 times, how to shorten it?
#3
with few informations and nothing to test, i would propose the following snippet:

import re
from _Arm64HexDatabase import *


inputVar = [
            "hex_suba64w0w0w0", 
            "hex_suba64w31w31w31", 
            "hex_suba64x0x0x0", 
            "hex_suba64x31x31x31", 
            ...
            ]

outputVar = [
             "hex_mova64w0c0", 
             "hex_mova64w31c0", 
             "hex_mova64x0c0", 
             "hex_mova64x31c0", 
             ...
             ]
# note: the 2 lists might be written once (and for all) in a file and loaded
#       the snippet assumes the pattern is exactly this one (otherwise if .. then ... must be used)

i = 0
with open ("example1", "rb") as handle_file1, open("result1.txt", "a") as hfile3:
    inputLine = handle_file1.readline()

    while (inputLine != ''):
        outputLine = re.sub(inputVar[i], outputVar[i], inputLine)
        hfile3.write(f"{outputLine}\n")
        i += 1
        inputLine = handle_file1.readline()
Reply


Messages In This Thread
RE: If I need to do regex replacement 27000 times, how to shorten it? - by paul18fr - Dec-24-2024, 10:14 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  i want to shorten a working section pizzakafz 15 2,687 Aug-23-2024, 11:56 AM
Last Post: deanhystad
  replacement for fpformat.extract GbSig1998 4 1,284 Apr-12-2024, 06:15 PM
Last Post: deanhystad
  String replacement in DB WJSwan 0 1,195 Dec-28-2022, 05:31 AM
Last Post: WJSwan
  python multiple try except block in my code -- can we shorten code mg24 10 14,737 Nov-10-2022, 12:48 PM
Last Post: DeaD_EyE
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 13,630 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Want to shorten the python code shantanu97 3 2,038 Apr-25-2022, 01:12 PM
Last Post: snippsat
  Extract the largest value from a group without replacement (beginner) preliator 1 2,635 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,726 Jun-30-2020, 06:38 PM
Last Post: asaxty
  line replacement help mdalireza 8 4,818 Nov-11-2019, 12:54 PM
Last Post: mdalireza
  xml replacement with python josesalazmit 3 9,717 Feb-24-2019, 07:28 PM
Last Post: stullis

Forum Jump:

User Panel Messages

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