1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
from tkinter import Tk
from tkinter import filedialog
import os
#######################
#C'est pour cette première partie qu'une manipulation doit être possible pour Linux j'imagine
root = Tk()
root.withdraw()
current_directory = filedialog.askdirectory()
select_file = filedialog.askopenfilenames()
file_path = os.path.join(current_directory,str(select_file))
print(file_path)
#######################
import re
###Ensemble des modifications dans le fichier que je n'ai pas détaillé car inutile pour la compréhension du code
dict = {'C':{'test1':'test2'},}
####
def find_END(line):
if re.search(r'\bFIN\b', line):
return True
else:
return False
def replacer_factory1(dictionary):
def replacing(match):
if len(dictionary) > 0:
word = match.group()
exchange = dictionary.get(word, word)
return exchange
else:
return ""
return replacing
def replacing1(text, dict):
regex_patt_list = r'\b(?:' + '|'.join(dict) + r')\b'
replacer = replacer_factory1(dict)
return re.sub(regex_patt_list, replacer, text)
with open(file_path) as fin:
with open(file_path + 'new', 'w') as fout:
key = 0
flag = 0
sous_dict = dict["C"]
cache_mem = []
for line in fin:
cache_mem.append(line)
if find_END(line):
#print(cache_mem)
for x in cache_mem:
if x.strip() in dict.keys():
key = x.strip()
sous_dict = dict[key]
fout.write(replacing1(x,sous_dict))
cache_mem = [] |
Partager