def read_cc_full_invoice(): import re from datetime import date, datetime import locale locale.setlocale(locale.LC_ALL, 'pt_BR.UTF-8') # Open the text file with open('OUROCARD_VISA_INFINITE-Ago_24.txt', 'r', encoding='latin') as file: # Read the contents of the file contents = file.readlines() # Define the regex patterns dan_pattern = r'1 - DANIEL.*' iza_pattern = r'4 - IZABELY.*' line_pattern = r'\d{2}\.\d{2}\.\d{4}.{23}.{14}.{2}\s*\d+,\d{2}\s*\d+,\d{2}' line_group_pattern = r'(\d{2})\.(\d{2})\.(\d{4})(.{23})(.{14})(.{2})(\s*\d+,\d{2})(\s*\d+,\d{2})' # Lists list_dan = [] list_iza = [] current_list = None insert_bulk = [] # Iterate all lines for line in contents: line = line.strip() if re.match(dan_pattern, line): current_list = 'list_dan' print('found Dan') elif re.match(iza_pattern, line): current_list = 'list_iza' print('found Iza') else: if re.match(line_pattern, line): if current_list == 'list_dan': print("dan", line) list_dan.append(line) if current_list == 'list_iza': print("iza", line) list_iza.append(line) print('list_dan - tuples for insert') for item in list_dan: match = re.search(line_group_pattern, item) tTdate = str(date(int(match.group(3)), int(match.group(2)), int(match.group(1)))) tAccount = 1 tMemo = match.group(4) tCity = match.group(5) tCountry = match.group(6) tOutflow = match.group(7).strip().replace(',', '.') tInflow = match.group(8).strip().replace(',', '.') tOwner = 1 tInstallments = 1 tCreated = str(datetime.now(tz=None)) tUpdated = None insert_bulk.append(( tTdate, tAccount, tMemo, tCity, tCountry, tOutflow, tInflow, tOwner, tInstallments, tCreated, tUpdated )) print('list_dan - tuples for insert') for item in list_iza: match = re.search(line_group_pattern, item) tTdate = str(date(int(match.group(3)), int(match.group(2)), int(match.group(1)))) tAccount = 1 tMemo = match.group(4) tCity = match.group(5) tCountry = match.group(6) tOutflow = match.group(7).strip().replace(',', '.') tInflow = match.group(8).strip().replace(',', '.') tOwner = 2 tInstallments = 1 tCreated = str(datetime.now(tz=None)) tUpdated = None insert_bulk.append(( tTdate, tAccount, tMemo, tCity, tCountry, tOutflow, tInflow, tOwner, tInstallments, tCreated, tUpdated )) return insert_bulk