如何从一个文本文件引用数据到另 一个文本文件

问题描述 投票:1回答:2

我仍然停留在这里,我正在尝试打印工资单。我想参考employee_info.txt以根据员工编号获取时间记录。但是我的问题是费率在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.x list reference text-files element
2个回答
1
投票
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的值,然后在写入文件时使用它。


1
投票

代码:

def employeInformation():
    employeeRecord = {}
    with open("employee_info.txt", 'r') as file:
        for dl in file:
            dl = dl.strip()
            if len(dl) >= 1:
                li = dl.split(",")
                emp_no = li[0].strip()
                first_name = li[1].strip()
                last_name = li[2].strip()
                department= li[3].strip()
                rate_per_day = li[4].upper().rstrip(';')
                #print(emp_no, first_name, last_name, department, rate_per_day)
                employeeRecord[emp_no] = [first_name, last_name, department, rate_per_day]
    return employeeRecord

def employeeTimeRecord(employeeRecord):
    with open("time_record.txt", 'r') as files:
        for dln in files:
            dln = dln.strip()
            if len(dln) >= 1:
                lii = dln.split(",")
                MR_empno = lii[0].strip()
                month = lii[1].strip()
                totalDays = lii[2].rstrip(';')
                if MR_empno in employeeRecord :
                    firstName = employeeRecord.get(MR_empno, None)[0]
                    lastName = employeeRecord.get(MR_empno, None)[1]
                    department = employeeRecord.get(MR_empno, None)[2]
                    ratePerDay = employeeRecord.get(MR_empno, None)[3]
                    gross = int(ratePerDay) * int(totalDays)
                    #print(MR_empno,ratePerDay, totalDays, gross)
                    print("Payslip for the MONTH OF:\t", month)
                    print("Employee No: "+str(MR_empno)+ ",\tEmployee Name: "+str(firstName) + " " +str(lastName))
                    print("Department:\t", department)
                    print("Rate Per Day: "+str(ratePerDay)+", \tNo. of Hours Worked: " + (totalDays))
                    print("Gross Pay:\t", gross)
                    print("*" * 160)

employeeRecord = employeInformation()
employeeTimeRecord(employeeRecord)

输出:

Payslip for the MONTH OF:    1
Employee No: 201911007, Employee Name: James Butt
Department:  Accounting
Rate Per Day: 365,  No. of Hours Worked: 28
Gross Pay:   10220
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 201203008, Employee Name: Josephine Darakjy
Department:  Marketing
Rate Per Day: 365,  No. of Hours Worked: 28
Gross Pay:   10220
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 199710014, Employee Name: Art Venere
Department:  Human Resources
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 201612010, Employee Name: Lenna Paprocki
Department:  Marketing
Rate Per Day: 565,  No. of Hours Worked: 28
Gross Pay:   15820
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 201710017, Employee Name: Donette Foller
Department:  Admin
Rate Per Day: 450,  No. of Hours Worked: 28
Gross Pay:   12600
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 201701013, Employee Name: Simona Morasca
Department:  Finance
Rate Per Day: 450,  No. of Hours Worked: 28
Gross Pay:   12600
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 201011003, Employee Name: Mitsue Tollner
Department:  Marketing
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 201409015, Employee Name: Leota Dilliard
Department:  Finance
Rate Per Day: 365,  No. of Hours Worked: 28
Gross Pay:   10220
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 199512017, Employee Name: Sage Wieser
Department:  MIS
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    1
Employee No: 199708003, Employee Name: Kris Marrier
Department:  Admin
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 201911007, Employee Name: James Butt
Department:  Accounting
Rate Per Day: 365,  No. of Hours Worked: 28
Gross Pay:   10220
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 201203008, Employee Name: Josephine Darakjy
Department:  Marketing
Rate Per Day: 365,  No. of Hours Worked: 28
Gross Pay:   10220
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 199710014, Employee Name: Art Venere
Department:  Human Resources
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 201612010, Employee Name: Lenna Paprocki
Department:  Marketing
Rate Per Day: 565,  No. of Hours Worked: 28
Gross Pay:   15820
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 201710017, Employee Name: Donette Foller
Department:  Admin
Rate Per Day: 450,  No. of Hours Worked: 28
Gross Pay:   12600
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 201701013, Employee Name: Simona Morasca
Department:  Finance
Rate Per Day: 450,  No. of Hours Worked: 28
Gross Pay:   12600
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 201011003, Employee Name: Mitsue Tollner
Department:  Marketing
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 201409015, Employee Name: Leota Dilliard
Department:  Finance
Rate Per Day: 365,  No. of Hours Worked: 28
Gross Pay:   10220
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 199512017, Employee Name: Sage Wieser
Department:  MIS
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    2
Employee No: 199708003, Employee Name: Kris Marrier
Department:  Admin
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    3
Employee No: 201911007, Employee Name: James Butt
Department:  Accounting
Rate Per Day: 365,  No. of Hours Worked: 10
Gross Pay:   3650
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    3
Employee No: 201203008, Employee Name: Josephine Darakjy
Department:  Marketing
Rate Per Day: 365,  No. of Hours Worked: 27
Gross Pay:   9855
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    3
Employee No: 199710014, Employee Name: Art Venere
Department:  Human Resources
Rate Per Day: 750,  No. of Hours Worked: 28
Gross Pay:   21000
****************************************************************************************************************************************************************
Payslip for the MONTH OF:    3
Employee No: 201612010, Employee Name: Lenna Paprocki
Department:  Marketing
Rate Per Day: 565,  No. of Hours Worked: 19
Gross Pay:   10735
****************************************************************************************************************************************************************
© www.soinside.com 2019 - 2024. All rights reserved.