首页 > 解决方案 > 如何将一个文本文件中的数据引用到另一个文本文件

问题描述

我仍然被困在这里,我正在尝试打印工资单。我想参考employee_info.txt根据员工编号获取时间记录。但我的问题是 rate 在 employee_info 中,days_work 在 time_record.txt 中,主要问题是它只打印一个人或一个月。

employee_info.txt 包含员工编号、姓氏、姓名、部门、​​每小时费率

201911007,James,Butt,Accounting,365;
201203008,Josephine,Darakjy,Marketing,365;
199710014,Art,Venere,Human Resources,750;
201612010,Lenna,Paprocki,Marketing,565;
201710017,Donette,Foller,Admin,450;
201701013,Simona,Morasca,Finance,450;
201011003,Mitsue,Tollner,Marketing,750;
201409015,Leota,Dilliard,Finance,365;
199512017,Sage,Wieser,MIS,750;
199708003,Kris,Marrier,Admin,750;
20011234, Robert, John, Finance, 120;

time_record.txt 包含员工编号、月份(数字)、工作天数

201911007,1,28;
201203008,1,28;
199710014,1,28;
201612010,1,28;
201710017,1,28;
201701013,1,28;
201011003,1,28;
201409015,1,28;
199512017,1,28;
199708003,1,28;
201911007,2,28;
201203008,2,28;
199710014,2,28;
201612010,2,28;
201710017,2,28;
201701013,2,28;
201011003,2,28;
201409015,2,28;
199512017,2,28;
199708003,2,28;
201911007,3,10;
201203008,3,27;
199710014,3,28;
201612010,3,19;

我的代码是。

def printing(last_name,first_name,month,time_record_empno,emp_no,department,rate_per_day,days_work):
    emp_no = []  #employee number as reference
    first_name = []
    last_name = []
    department = []
    rate_per_day = []
    time_record_empno=[]  # employee number 
    month=[]
    days_work=[]

    with open("employee_info.txt", 'r') as file:
        for dl in file:
            dl = dl.strip()
            if len(dl) >= 1:
                li = dl.split(",")
                emp_no.append(li[0].strip())
                first_name.append(li[1].strip())
                last_name.append(li[2].strip())
                department.append(li[3].strip())
                rate_per_day.append(li[4].upper().rstrip(';'))
    with open("time_record.txt", 'r') as files:
        for dln in files:
            dln = dln.strip()
            if len(dln) >= 1:
                lii = dln.split(",")
                MR_empno.append(lii[0].strip())
                month.append(lii[1].strip())
                days_work.append(lii[2].rstrip(';'))
       for mm, mr, ll, nn,  emp,  dep, rat, hr in zip(month, empno, last, name, mrem, depart, rates, hours):
           files = open('payslip.txt', 'w')
           print("*" * 160)
           print(f"Payslip for the MONTH OF:{mm}")
           print(f"Employee No.{mr}Employee Name:{ll}, {nn} ")
           print(f"Department:{dep}")
           print(f"Rate Per Day:{rat}No. of Hours Worked:{hr} ")
           gross = int(rat)*int(hr)
           print(f"Gross Pay:{gross}")
           print("*" * 160)

有什么建议可以解决这个问题吗?谢谢你。

标签: python-3.xlistreferencetext-fileselement

解决方案


def printing():
  emp_no = []  #employee number as reference
  first_name = []
  last_name = []
  department = []
  rate_per_day = []
  month=[]
  days_work=[]
  mremp_no = []

  with open("a.txt", 'r') as file:
      for dl in file:
          dl = dl.strip()
          if len(dl) >= 1:
              li = dl.split(",")
              emp_no.append(li[0].strip())
              first_name.append(li[1].strip())
              last_name.append(li[2].strip())
              department.append(li[3].strip())
              rate_per_day.append(li[4].upper().rstrip(';'))

  dict_emp = {}

  with open("b.txt", 'r') as files:
      for dln in files:
          dln = dln.strip()
          if len(dln) >= 1:
              lii = dln.split(",")
              temp1 = lii[0].strip()
              temp2 = lii[1].strip()
              temp3 = lii[2].rstrip(';')
              if temp1 not in dict_emp:
                dict_emp[temp1] = [(temp2, temp3)]
              else:
                dict_emp[temp1].append((temp2, temp3))
              # mremp_no.append(lii[0].strip())
              # month.append(lii[1].strip())
              # days_work.append(lii[2].rstrip(';'))

  print(dict_emp)

  for en, ff, ll, depart, rate in zip(emp_no, first_name, last_name, department, rate_per_day):
    print("*" * 20)
    print(f"Employee No.{en} Employee Name:{ll}, {ff} ")
    print(f"Department:{depart}")
    print(f"Rate Per Day:{rate}")
    if en in dict_emp:
      for mon, num_days in dict_emp[en]:
        print('\t'+'#'*10)
        print(f"\tPayslip for the MONTH OF:{mon} ")
        print(f"\tGross Pay:{int(rate) * int(num_days)} ")
    print("*" * 20)
    print('\n')

  with open('payslip.txt', 'w') as filee:
    for en, ff, ll, depart, rate in zip(emp_no, first_name, last_name, department, rate_per_day):
      filee.write("*" * 20)
      filee.write('\n')
      filee.write(f"Employee No.{en} Employee Name:{ll}, {ff} ")
      filee.write('\n')
      filee.write(f"Department:{depart}")
      filee.write('\n')
      filee.write(f"Rate Per Day:{rate}")
      filee.write('\n')
      if en in dict_emp:
        for mon, num_days in dict_emp[en]:
          filee.write('\t'+'-'*10)
          filee.write('\n')
          filee.write(f"\tPayslip for the MONTH OF:{mon} ")
          filee.write('\n')
          filee.write(f"\tGross Pay:{int(rate) * int(num_days)} ")
          filee.write('\n')
      filee.write("*" * 20)
      filee.write('\n')
      filee.write('\n')
printing()

输出

********************
Employee No.201911007 Employee Name:Butt, James 
Department:Accounting
Rate Per Day:365
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:10220 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:10220 
    ----------
    Payslip for the MONTH OF:3 
    Gross Pay:3650 
********************

********************
Employee No.201203008 Employee Name:Darakjy, Josephine 
Department:Marketing
Rate Per Day:365
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:10220 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:10220 
    ----------
    Payslip for the MONTH OF:3 
    Gross Pay:9855 
********************

********************
Employee No.199710014 Employee Name:Venere, Art 
Department:Human Resources
Rate Per Day:750
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:21000 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:21000 
    ----------
    Payslip for the MONTH OF:3 
    Gross Pay:21000 
********************

********************
Employee No.201612010 Employee Name:Paprocki, Lenna 
Department:Marketing
Rate Per Day:565
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:15820 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:15820 
    ----------
    Payslip for the MONTH OF:3 
    Gross Pay:10735 
********************

********************
Employee No.201710017 Employee Name:Foller, Donette 
Department:Admin
Rate Per Day:450
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:12600 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:12600 
********************

********************
Employee No.201701013 Employee Name:Morasca, Simona 
Department:Finance
Rate Per Day:450
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:12600 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:12600 
********************

********************
Employee No.201011003 Employee Name:Tollner, Mitsue 
Department:Marketing
Rate Per Day:750
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:21000 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:21000 
********************

********************
Employee No.201409015 Employee Name:Dilliard, Leota 
Department:Finance
Rate Per Day:365
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:10220 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:10220 
********************

********************
Employee No.199512017 Employee Name:Wieser, Sage 
Department:MIS
Rate Per Day:750
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:21000 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:21000 
********************

********************
Employee No.199708003 Employee Name:Marrier, Kris 
Department:Admin
Rate Per Day:750
    ----------
    Payslip for the MONTH OF:1 
    Gross Pay:21000 
    ----------
    Payslip for the MONTH OF:2 
    Gross Pay:21000 
********************

********************
Employee No.20011234 Employee Name:John, Robert 
Department:Finance
Rate Per Day: 120
********************

使用字典存储 emp_no、month、days_worked 的值,然后在写入文件时使用它。


推荐阅读