Python提取相同格式日期间交易数据的技术问题求助
交易记录提取问题
需求:使用Python从文本中提取有效交易记录,每笔交易以dd/mm/yyyy格式日期开头,截止到下一个同格式日期前,交易可包含1条或多条描述信息。
单笔交易示例
03/04/2024 Payments / Collections 10.08 795.04 ---> Date description cost balance HU INSURANCE UK ---> Description G0003406201 56171304 2024-04-17 ---> Description G3406201 ---> Description
输入文本
input_text = """ 02/04/2024 Funds Transfer 56.00 1,805.12 TOP-UP TO WALLET! : 84343571729 02/04/2024 Bill Payment 1,000.00 805.12 UHJN-5520380040396554 : I-BANK 03/04/2024 Payments / Collections 10.08 795.04 HU INSURANCE UK G0003406201 56171304 2024-04-17 G3406201 04/04/2024 FAST Payment / Receipt 12,000.00 12,795.04 INVEST 20240404CIBBSTSTBRT3273519 OTHER 04/04/2024 Bill Payment 333.00 12,462.04 GBU -09890340922 : I-BANK 30/04/2024 Interest Earned 0.18 2,385.42 """
现有代码
import re # Regex pattern for dates (dd/mm/yyyy) date_pattern = r"\d{2}/\d{2}/\d{4}" # Find all date matches date_matches = re.findall(date_pattern, input_text) # Initialize an empty list to store desired output output = [] # Extract text between dates for i in range(len(date_matches) - 1): start_index = input_text.find(date_matches[i]) + len(date_matches[i]) end_index = input_text.find(date_matches[i + 1]) text_between_dates = input_text[start_index:end_index].strip() output.append([date_matches[i], text_between_dates]) # Print the desired output for item in output: print(item)
当前输出结果
['02/04/2024', ''] ['02/04/2024', 'Funds Transfer 56.00 1,805.12\nTOP-UP TO WALLET! :\n84343571729\n02/04/2024 Bill Payment 1,000.00 805.12\nUHJN-5520380040396554 : I-BANK'] ['03/04/2024', 'Payments / Collections 10.08 795.04\nHU INSURANCE UK\nG0003406201 56171304 2024-04-17\nG3406201'] ['04/04/2024', ''] ['04/04/2024', 'FAST Payment / Receipt 12,000.00 12,795.04\nINVEST\n20240404CIBBSTSTBRT3273519\nOTHER\n04/04/2024 Bill Payment 333.00 12,462.04\nGBU -09890340922 : I-BANK']
期望输出结果
['02/04/2024', 'Funds Transfer 56.00 1,805.12', 'TOP-UP TO WALLET! :', '84343571729'] ['02/04/2024', 'Bill Payment 1,000.00 805.12', 'UHJN-5520380040396554 : I-BANK'] ['03/04/2024', 'Payments / Collections 10.08 795.04', 'HU INSURANCE UK', 'G0003406201 56171304 2024-04-17', 'G3406201'] ['04/04/2024', 'FAST Payment / Receipt 12,000.00 12,795.04', 'INVEST', '20240404CIBBSTSTBRT3273519', 'OTHER'] ['04/04/2024', 'Bill Payment 333.00 12,462.04', 'GBU -09890340922 : I-BANK'] ['30/04/2024', 'Interest Earned 0.18 2,385.42']
解决方案
现有代码的问题在于使用input_text.find(date_matches[i])时,相同日期会匹配到第一个出现的位置,导致分割逻辑混乱。正确的做法是获取所有日期的精确位置,然后按位置分割文本,再处理每段交易内容:
修正后的代码
import re input_text = """ 02/04/2024 Funds Transfer 56.00 1,805.12 TOP-UP TO WALLET! : 84343571729 02/04/2024 Bill Payment 1,000.00 805.12 UHJN-5520380040396554 : I-BANK 03/04/2024 Payments / Collections 10.08 795.04 HU INSURANCE UK G0003406201 56171304 2024-04-17 G3406201 04/04/2024 FAST Payment / Receipt 12,000.00 12,795.04 INVEST 20240404CIBBSTSTBRT3273519 OTHER 04/04/2024 Bill Payment 333.00 12,462.04 GBU -09890340922 : I-BANK 30/04/2024 Interest Earned 0.18 2,385.42 """ date_pattern = r"\d{2}/\d{2}/\d{4}" # 获取所有日期的匹配对象,包含起始和结束索引 date_matches = list(re.finditer(date_pattern, input_text)) output = [] # 遍历每一个日期匹配 for i in range(len(date_matches)): current_match = date_matches[i] current_date = current_match.group() # 确定当前交易的结束位置 if i < len(date_matches) - 1: end_pos = date_matches[i+1].start() else: end_pos = len(input_text) # 提取当前交易的所有文本(从日期结束到下一个日期开始) transaction_text = input_text[current_match.end():end_pos].strip() # 按换行分割,去掉空行,整理成非空的描述列表 descriptions = [line.strip() for line in transaction_text.split('\n') if line.strip()] # 组合成交易条目:日期 + 所有描述 transaction = [current_date] + descriptions output.append(transaction) # 打印结果 for item in output: print(item)
代码说明
- 使用
re.finditer替代re.findall,获取每个日期的匹配对象,包含精确的起始和结束索引,避免重复日期的位置错误。 - 遍历每个日期匹配,根据下一个日期的起始位置确定当前交易的文本范围;最后一笔交易则取到文本末尾。
- 将交易文本按换行分割,过滤掉空行,得到所有非空的描述内容。
- 将日期和描述内容组合成列表,加入输出结果。
运行输出
['02/04/2024', 'Funds Transfer 56.00 1,805.12', 'TOP-UP TO WALLET! :', '84343571729'] ['02/04/2024', 'Bill Payment 1,000.00 805.12', 'UHJN-5520380040396554 : I-BANK'] ['03/04/2024', 'Payments / Collections 10.08 795.04', 'HU INSURANCE UK', 'G0003406201 56171304 2024-04-17', 'G3406201'] ['04/04/2024', 'FAST Payment / Receipt 12,000.00 12,795.04', 'INVEST', '20240404CIBBSTSTBRT3273519', 'OTHER'] ['04/04/2024', 'Bill Payment 333.00 12,462.04', 'GBU -09890340922 : I-BANK'] ['30/04/2024', 'Interest Earned 0.18 2,385.42']
内容的提问来源于stack exchange,提问作者snow
相关产品推荐
相关产品推荐

