jaVer404

Final

Jul 28th, 2024
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. import re
  2.  
  3. def split_pages(file_path):
  4.     # Відкриваємо файл з кодуванням cp1251
  5.     with open(file_path, 'r', encoding='cp1251') as file:
  6.         text = file.read()
  7.  
  8.     pages = text.split('\x0c')
  9.     return pages
  10.  
  11.  
  12. def has_matching_fourth_line(page):
  13.     # Регулярний вираз для перевірки
  14.     pattern = re.compile(r'^.*".*".*$')
  15.     lines = page.splitlines()
  16.     if len(lines) >= 4 and pattern.match(lines[3]):
  17.         return True
  18.     else:
  19.         return False
  20.  
  21.  
  22.  
  23. def contains_any_element(elements, text):
  24.     for element in elements:
  25.         if element in text:
  26.             return True
  27.     return False
  28.  
  29. def append_array_to_txt(elements, file_path_result):
  30.     with open(file_path_result, 'a', encoding='cp1251') as file_result:
  31.         for element in elements:
  32.             file_result.write(element)
  33.  
  34.  
  35. def remove_crlf_lines(input_file_path, output_file_path):
  36.     # Читаємо вміст вихідного файлу
  37.     with open(input_file_path, 'r', encoding='cp1251') as file:
  38.         lines = file.readlines()
  39.  
  40.     # Фільтруємо рядки, які не містять лише символи CRLF
  41.     filtered_lines = [line for line in lines if line.strip() != '']
  42.  
  43.     # Записуємо відфільтрований вміст у новий файл
  44.     with open(output_file_path, 'w', encoding='cp1251') as file:
  45.         file.writelines(filtered_lines)
  46.  
  47. banks = [
  48. 'А - БАНК',
  49. 'А-БАНК',
  50. 'ІДЕЯ БАНК',
  51. 'КРИСТАЛБАНК',
  52. 'СЕНС БАНК',
  53. ]
  54.  
  55. # Виклик функції
  56. file_path = 'd:\\test\\bank\\bank.txt'
  57. file_path_rusult = 'd:\\test\\bank\\bank_result.txt'
  58. file_path_rusult_clean = 'd:\\test\\bank\\bank_result_clean.txt'
  59.  
  60. pages = split_pages(file_path)
  61. #has_matching_fourth_line(pages)
  62. #print ("Number of pages: " + str ((len(pages))))
  63.  
  64. #print(result)
  65.  
  66. cleanPages = []
  67.  
  68. flag = "no"
  69.  
  70. for element in pages:
  71.     #перевіряємо сторінку на наявність регулярного виразу
  72.     if has_matching_fourth_line(element):
  73.         if contains_any_element(banks,element):
  74.             flag = "yes"
  75.             cleanPages.append(element + '\x0c')
  76.         else:
  77.             flag = "no"
  78.     else:
  79.         if flag == "yes":
  80.             cleanPages.append(element + '\x0c')
  81.  
  82.  
  83.  
  84. append_array_to_txt(cleanPages,file_path_rusult)
  85.  
  86. remove_crlf_lines(file_path_rusult,file_path_rusult_clean)
Advertisement
Add Comment
Please, Sign In to add comment