Advertisement
nicuf

Multiple finds and replace with Python

Aug 14th, 2021 (edited)
1,357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. MORE DETAILS HERE:
  2. ENGLISH:  https://neculaifantanaru.com/en/python-regex-batch-processor-multiple-find-and-replace.html
  3. ROMANIAN:  https://neculaifantanaru.com/python-regex-batch-processor-multiple-find-and-replace.html
  4.  
  5.  
  6. # How to do multiple finds and replace with Python
  7.  
  8. # The Python code below will do 3 Regex operations (Find + Replace), in the order I chose it.
  9. # Option 1 - Find and Replace in a single .txt file
  10.  
  11. import re
  12. import os
  13.  
  14. with open('Python.txt','r') as f:
  15.     text_citit_din_fisier = f.read()
  16.  
  17. print("hello here: ", text_citit_din_fisier)
  18.  
  19. # Regex1  FIND: <link (.*).*(href.*")  REPLACE BY: <link \2 \1
  20. text_citit_din_fisier=re.sub(r'<link (.*).*(href.*")', r'<link \2 \1', text_citit_din_fisier)
  21. print ("Primul Regex:", text_citit_din_fisier)
  22.  
  23. # Regex2  FIND: spree.*>  REPLACE BY: bebe
  24. text_citit_din_fisier=re.sub(r'spree.*>', r'bebe', text_citit_din_fisier)
  25. print ("Second Regex:", text_citit_din_fisier)
  26.  
  27. # Regex3  FIND: (.*)bebe  REPLACE BY: empty row/x20
  28. text_citit_din_fisier=re.sub(r'(.*)bebe', r'empty row', text_citit_din_fisier)
  29. print ("Third Regex:", text_citit_din_fisier)
  30.  
  31. with open("Python.txt", "w") as some_file_handle:
  32.     some_file_handle.write(text_citit_din_fisier)
  33.  
  34.  
  35. ######################################
  36. # Option 2 - Find and Replace in all .txt files from folder
  37. ######################################
  38. ######################################
  39.  
  40.  
  41. import re
  42. import os
  43.  
  44. # 0. Construim o functie care primeste ca argument un fisier si aplica niste expresii regulate
  45. def aplica_expresii_regulate(cale_fisier):
  46.     with open(cale_fisier,'r') as f:
  47.         text_citit_din_fisier = f.read()
  48.  
  49. # Regex1  FIND: <link (.*).*(href.*")  REPLACE BY: <link \2 \1
  50. text_citit_din_fisier=re.sub(r'<link (.*).*(href.*")', r'<link \2 \1', text_citit_din_fisier)
  51. print ("Primul Regex:", text_citit_din_fisier)
  52.  
  53. # Regex2  FIND: spree.*>  REPLACE BY: bebe
  54. text_citit_din_fisier=re.sub(r'spree.*>', r'bebe', text_citit_din_fisier)
  55. print ("Second Regex:", text_citit_din_fisier)
  56.  
  57. # Regex3  FIND: (^.*)bebe  REPLACE BY: empty row/x20
  58. text_citit_din_fisier=re.sub(r'.*bebe', r'empty row', text_citit_din_fisier)
  59. print ("Third Regex:", text_citit_din_fisier)  
  60.    
  61.  
  62.     print(text_citit_din_fisier)
  63.    
  64.     with open(cale_fisier, "w") as h:
  65.         h.write(text_citit_din_fisier)
  66.  
  67.  
  68. # 1. Construim o functie care primeste ca argument un director, iar pentru fiecare fisier din director facem o anumita operatie
  69. def parcurge_director(cale_director):
  70.     for nume_fisier in os.listdir(cale_director):
  71.         if nume_fisier.endswith(".txt") or nume_fisier.endswith(".png"): #daca incepe cu txt
  72.             cale_completa_fisier = os.path.join(cale_director, nume_fisier)
  73.             aplica_expresii_regulate(cale_completa_fisier)
  74.         else:
  75.             continue
  76.  
  77.  
  78. directory = r'd:\Downloads'
  79. parcurge_director(directory)
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement