【问题标题】:Regex from a list of strings in PythonPython中字符串列表中的正则表达式
【发布时间】:2020-06-29 12:23:06
【问题描述】:

我有一个名为 Statement 的列表,使用 pytesseract 和 Regex 从 pdf 创建:

Statement= ['07-10-2019 UPI/927912685773/UPI/surya.balaji94@/Citibank 6,677.00 2,36,804.08',
         '07-10-2019 MOBILE BANKING DUT/CITIBANK 3,403.00 2,40,207.08',
         '07-10-2019 BIL/INFT/001818195728/82D3/ AJAY KUMAR JHA 6,080.00 2,46, 287.08',
         '08.10.2019 MOBILE BANKING MMM TiMPS/928115182374/8161 Oct Mte/AMARJEET SIHDFC 4,411.00 250,698.08',
         '08-10-2019 BIL/INFT/001818636132/E3 BIk1 Pramod/ PRAMOD KUMAR P 6,599.00 2,57,297.08']

在堆栈上的一些帮助下,我创建了一个字典列表,如下所示:

cols = ["Date", "Item_Name", "Transaction_Amount", "Balance"]
date_pattern = re.compile(r"\d{2}[- /.]\d{2}[- /.]\d{4}", re.I)
item_and_name_pattern = re.compile(r"(?<=\d{2}-\d{2}-\d{4}\s).*", re.I)
amount_pattern = re.compile(r"\d+,\d+.\d+", re.I)
total_pattern = re.compile(r"\d+,\d+,\d+.\d+$", re.I)
Transaction = namedtuple("Transaction", cols)
transactions = []
for item in Statement:
    try:
        date = re.search(date_pattern, item).group()
        total = re.search(total_pattern, item).group()
        temp_1 = item.rstrip(total)
        amount = re.search(amount_pattern, item).group()
        temp_2 = temp_1.strip().rstrip(amount)
        item_and_name = re.search(item_and_name_pattern, temp_2).group()
    except:
        pass
    t = Transaction(date, item_and_name, amount, total)
    transactions.append(t)
    out = [{k:v for k, v in f._asdict().items()} for f in transactions]

但输出并不令人满意,因为它正在获取日期,但该日期的项目名称和总数等出错(检查上面的列表并与下面的字典匹配)。我想知道是否有任何其他方法可以正确地将它们存储在命名列中?

    [{'Date': '07-10-2019',
  'Item_Name': 'UPI/927912685773/UPI/surya.balaji94@/Citibank ',
  'Transaction_Amount': '6,677.00',
  'Balance': '2,36,804.08'},
 {'Date': '07-10-2019',
  'Item_Name': 'MOBILE BANKING DUT/CITIBANK ',
  'Transaction_Amount': '3,403.00',
  'Balance': '2,40,207.08'},
 {'Date': '07-10-2019',
  'Item_Name': 'MOBILE BANKING DUT/CITIBANK ',
  'Transaction_Amount': '3,403.00',
  'Balance': '2,40,207.08'},
 {'Date': '08.10.2019',
  'Item_Name': 'MOBILE BANKING DUT/CITIBANK ',
  'Transaction_Amount': '3,403.00',
  'Balance': '2,40,207.08'},
 {'Date': '08-10-2019',
  'Item_Name': 'BIL/INFT/001818636132/E3 BIk1 Pramod/ PRAMOD KUMAR P ',
  'Transaction_Amount': '6,599.00',
  'Balance': '2,57,297.08'}]
    

【问题讨论】:

  • 哦哈哈!当然..

标签: python-3.x regex list dictionary namedtuple


【解决方案1】:

这是一个更简单的方法:

import re

pattern = re.compile("(?P<Date>\d{2}[.-]\d{2}[.-]\d{4})\s(?P<Item_Name>.+)\s(?P<Transaction_Amount>[0-9,\.]+)\s(?P<Balance>[0-9,\.]+)")

print([pattern.match(item).groupdict() for item in Statement])

编辑:如果按照 cmets 的要求使用 try-except:

result = []
for item in Statement:
    try:
        result.append(pattern.match(item).groupdict())
    except AttributeError:
          pass

print(result)

【讨论】:

  • 谢谢。如果列表很长并且某些模式不匹配,我想尝试,除了通过那里。有办法吗?
猜你喜欢
  • 2021-08-20
  • 2018-12-26
  • 2016-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 2018-01-02
相关资源
最近更新 更多