如何对文件执行多个regex搜索?

2024-09-27 21:24:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读一个xml文件,找到四位数据,然后在数据库中更新。我的问题是如何更有效地搜索它们。在这种情况下,一行一行是最好的吗?我正在把整个文件读入一个变量,然后对它进行搜索

with open(cur_file, 'rb') as xml_file:
    bill_mgr_email_re = re.compile(r'<BillingManagerInformation .* Email="(.*.com)"')
    num_bills_re = re.compile(r'NumberBills="(\d+)"')
    num_ebills_re = re.compile(r'NumberOfEbills="(\d+)"')
    num_mailed_re = re.compile(r'NumberOfMailedDocs="(\d+)"')
    data = xml_file.read()

    bill_mgr_email = bill_mgr_email_re.search(data).group(1)
    num_bills = num_bills_re.search(data).group(1)
    num_ebills = num_ebills_re.search(data).group(1)
    num_mailed = num_mailed_re.search(data).group(1)

Tags: 文件researchdataemailgroupxmlnum

热门问题