基于delimi多次出现的分割文件

2024-10-02 00:24:55 发布

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

我执行了以下任务,根据分隔符的多次出现来分割文件。
我有一个包含以下数据的文件:

Number of Employees       - 95

==============================================================================
 Telephone Number -  972123111111
 Empl Name  -   David
 Designation  - Software Engineer
 Address: **********
 Doamin: Python
==============================================================================
 Telephone Number -  972123111112
 Empl Name  - Glen
 Designation  - Software Engineer
 Doamin: Python
==============================================================================
 Telephone Number -  972123111111
 Empl Name  - Jhon
 Designation  - Software Engineer
 Address: **********
 Doamin: Python
==============================================================================

在这个文件中,我想将每个员工的信息拆分为“=”,然后打印所有员工的内容,如下所示:

Details of Employee: (Employee Name)
  Telephone Number: (employee telephone number)
  Designation : (employee desgination) 

我已经编写了将文件中的数据提取到变量中的代码,并使用下面的正则表达式对数据进行灰色化,但没有效果:

re.findall('[^=]=*.*?[=*$]', a)

Tags: 文件of数据namenumberaddress员工employee
2条回答

使用^{}而不是re.findall(),如下所示:

re.findall(r'^=+$', a)

试试这个片段,它将所有员工数据作为字典存储在一个完整的列表中

import re
data_separator_regepx     = "-|:" #theres - and : as separators in sample text
employee_separator_regexp ="^=+$"
employees      = []
with open('test.txt') as f_in:
    curr_employee = {}
    for idx,line in enumerate(f_in):
        if not idx : continue #skip first line
        line = line.strip()
        if not line: continue #skip empty lines
        if re.match(employee_separator_regexp,line):
            if curr_employee:
                employees.append(curr_employee)
                curr_employee = {}
        else:
            line = re.split(data_separator_regepx,line)
            key, value = line[0],line[1]
            curr_employee[key.strip()]=value.strip()

for employee in employees:
    print "Details of Employee: ({})".format(employee.get('Empl Name',''))
    print "Telephone Number: ({})".format(employee.get('Telephone Number',''))
    print "Designation: ({})".format(employee.get('Designation',''))

相关问题 更多 >

    热门问题