繁体   English   中英

从一组打印一个项目时,这条额外的线来自哪里? (CSV) 文件使用 Python 和 Spyder4

[英]Where is this extra line come from when printing an item from a set? (CSV) file Using Python and Spyder4

我对一组中的一个项目的 print(tmp[3]) function 感到困惑。 打印项目 tmp[3] 时,它给了我两行,其中一行是值,另一行是空的。 一切运行良好,直到第 132 行。

该程序的目标是打印一份工资汇总报告,该报告基于以前程序的信息(具有不同的费率)。 您应该使用单独的函数来执行以下操作:

计算正常工作时间(40 小时及以下) 计算加班时间(40 小时以上) 计算正常工资(正常时间乘以正常工资) 计算加班工资(加班时间乘以正常工资乘以 1.5) 计算总工资计算预扣的联邦税金额(总工资的 15.1%) 计算预扣的 state 税的金额(总工资的 5.5%) 计算预扣的医疗保险金额(总工资的 1.2%) 计算预扣的社会保障金额(4.8总工资的百分比)计算总扣除额(联邦税 + state 税 + 医疗保险 + 社会保障)计算净工资(总工资 - 扣除)计算总工资(每个人的总和)净工资打印(到屏幕上)一个干净的摘要报告(见上面的 output)打印(到屏幕上)总净支付。

我找到了基于输入的版本的函数,因此从 csv 中提取数据并在函数中使用它对我来说是一个挑战。

如果你想要一个更简单的复制和过去的程序类似于这个链接: 从 CSV 文件中读取记录并打印报告

print(tmp[3]) 的预期结果是“10”。 实际结果..“10”。 我不知道为什么它会打印额外的一行。 我认为这个问题的症状是无法计算正常工资 = Payrate * hoursWorked。 期望 output 为 420。 42(payrate) * 10(hours) = 420 这是第 133 行的错误“TypeError:can't multiply sequence by non-int of type 'float'” at line 132 这是一个家庭作业我只是想解决这个错误,所以我的整个程序似乎没有必要。

这是 csv 文件:

First, Last, Hours, Pay
Matthew, Hightower, 42, 10
Samuel, Jackson, 53, 12.58
Catherine, Jones, 35, 19.43

这是整个程序。 定义主():

results = get_data("employeestest.csv")

payrollSummaryReport(results)

def get_data(fname):

Function returns the dictionary with following 
format:
{ 0 : {
    "fname": "...",
    "lname": "...",
    "gross": "...",
  },
  1 : {
    ....,
    ,,,,
  },
}

result = {} # return value i = 0 # 你可以 zip range() if you want to with open(fname, 'r') as f:

  for line in f.readlines()[1:]:

      result[i] = {}
      tmp = line.split(",") # list of values from file 
      # access file values by their index, e.g. 
      # tmp[0] -> first name
      # tmp[1] -> last name
      # tmp[2] -> hours
      # tmp[3] -> pay rate
      employeeRegularHours, employeeOvertimeHours = calculateRegularHours(tmp[2])
      employeeOvertimeHours = calculateOvertimeHours(tmp[2])
      employeeTotalHours = calculateTotalHours(employeeRegularHours, employeeOvertimeHours)
      print(tmp[3])
This is were I print the item but it comes out to 
"10
       "
 I don't know why it skips a line. I found this error through this "TypeError:can't multiply sequence by non-int of type 'float'" at line 132

      #print(employeeRegularHours)
      regularPayAmount = calculateRegularPay(tmp[3], employeeRegularHours)
      overtimePayAmount = calculateOvertimePay(tmp[3], employeeOvertimeHours)
      grossPayAmount = calculateGrossPay(regularPayAmount, overtimePayAmount)
      federalTaxWithheld = calculateFederalTax(grossPayAmount)
      stateTaxWithheld = calculateStateTax(grossPayAmount)
      medicareTaxWithheld = calculateMedicareTax(grossPayAmount)
      socSecTaxWithheld = calculateSocSecTax(grossPayAmount)
      totalTaxesWithheld = calculateTotalTaxes(federalTaxWithheld, stateTaxWithheld, 
      medicareTaxWithheld, socSecTaxWithheld)
      netPayAmount = calculateNetPay(grossPayAmount, totalTaxesWithheld)
      #(calculateOvertimePay, calculateTotalHours) = 
      #etc.) and store the results in dictionary
      # e.g: 

      result[i]["fname"] = tmp[0]
      result[i]["lname"] = tmp[1]
      result[i]["hours"] = tmp[2]
      result[i]["payrate"] = tmp[3]
  # ...
      # do calculations for report
      # ...
      result[i]["regular"] = employeeRegularHours
      result[i]["overtime"] = employeeOvertimeHours
      result[i]["totalhours"] = employeeTotalHours
      result[i]["regPay"] = regularPayAmount
      result[i]["overPay"] = overtimePayAmount
      result[i]["gross"] = grossPayAmount
      result[i]["fedtax"] = federalTaxWithheld
      result[i]["stateTax"] = stateTaxWithheld
      result[i]["medTax"] = medicareTaxWithheld
      result[i]["socsectax"] = socSecTaxWithheld
      result[i]["totaltax"] = totalTaxesWithheld
      result[i]["netpay"] = netPayAmount
      i += 1
  return result

def calculateRegularHours(employeeHoursWorked) :
   #print(employeeHoursWorked)

   if float(employeeHoursWorked)  < 40.0 :
      employeeRegularHours = employeeHoursWorked
      employeeOvertimeHours = 0.0
   else:

     employeeRegularHours = 40.0
     employeeOvertimeHours = 0.0
    #employeeOvertimeHours = employeeHoursWorked - 40.0


   return employeeRegularHours, employeeOvertimeHours

def calculateOvertimeHours(employeeHoursWorked) :
   if float(employeeHoursWorked) > 40 :
       #float(employeeOvertimeHours) = employeeHoursWorked - 40
       #print(employeeHoursWorked)
       employeeOvertimeHours = 0.0
   else :
       employeeOvertimeHours = 0

   return employeeOvertimeHours

def calculateTotalHours(employeeRegularHours, employeeOvertimeHours) :
    employeeTotalHours = employeeRegularHours #+ employeeOvertimeHours
    return employeeTotalHours


def calculateRegularPay(employeePayRate, employeeHoursWorked) :

    regularPayAmount = employeePayRate * employeeHoursWorked
    return regularPayAmount

def calculateOvertimePay(employeePayRate, employeeOvertimeHours) :
    overtimePayRate = 1.5
    overtimePayAmount = (employeePayRate * employeeOvertimeHours) * 
    overtimePayRate
    return overtimePayAmount

def calculateGrossPay(regularPayAmount, overtimePayAmount) :
    grossPayAmount = regularPayAmount + overtimePayAmount
    return grossPayAmount

def calculateFederalTax(grossPayAmount) :
    federalTaxRate = 0.151
    federalTaxWithheld = grossPayAmount * federalTaxRate
    return federalTaxWithheld

def calculateStateTax(grossPayAmount) :
    stateTaxRate = 0.055
    stateTaxWithheld = grossPayAmount * stateTaxRate
    return stateTaxWithheld

def calculateMedicareTax(grossPayAmount) :
    medicareTaxRate = 0.012
    medicareTaxWithheld = grossPayAmount * medicareTaxRate
    return medicareTaxWithheld

def calculateSocSecTax(grossPayAmount) :
    socSecTaxRate = 0.048
    socSecTaxWithheld = grossPayAmount * socSecTaxRate
    return socSecTaxWithheld

def calculateTotalTaxes(federalTaxWithheld, stateTaxWithheld,         
    medicareTaxWithheld, socSecTaxWithheld) :
    totalTaxesWithheld = federalTaxWithheld + stateTaxWithheld +     
    medicareTaxWithheld + socSecTaxWithheld
    return totalTaxesWithheld

def calculateNetPay(grossPayAmount, totalTaxesWithheld) :
    netPayAmount = grossPayAmount - totalTaxesWithheld
    return netPayAmount

def payrollSummaryReport(vals):
    print()
    print("\t\t\t\t\t\tPayroll Summary Report")
    print("%-12s%-12s%-8s%-10s%-10s%-12s%-10s%-11s%-13s%-10s" %\
     ("LastName", "FirstName", "Hours", "RegHours", "OTHours", "RegPay", "OTPay", "GrossPay", "Deductions", "NetPay"))
    for i in vals:
       print("%-12s%-12s%-8.2f%-10.2f%-10.2f$%-11.2f$%-9.2f$%-10.2f$%-12.2f$%-10.2f" %\   

       (vals[i]["fname"], vals[i]["lname"], vals[i]["gross"]))

主要的()

                     [Expected Output][1]   

   Payroll Summary Report

姓 名 工作时间 正常工作时间 加班时间
Hightower Matthew 42.0 40.0 2.0 400.0 30.0 430.00 107.07 322.93 Jackson Samuel 53.0 40.0 13.0 506.0 246.68 752.67 187.42 565.25 Jones Catherine 35.0 35.0 0.0 680.05 0.0 680.05 169.33 510.72

总净工资 1,398.90

定期工资 OT 工资 总工资 扣除额 净工资

f.readlines 的f.readlines包括行尾 (EOL) 字符,因此如果您以逗号分隔行并打印最后一项,它将打印值行尾字符。 如果你做print(repr(tmp[3]))你会看到类似'10\n'东西。

您可以像这样从行中删除 EOL:

tmp = line.strip().split(",")

但是,由于您正在处理 csv 文件,您可以使用 Python 内置的csv模块,该模块将为您处理一些细节

import csv

# Do stuff

with open(fname, 'r', newline='') as f:
    reader = csv.reader(f)
    # Skip header row
    next(reader)

    # reader yield lists, but since we know each list will have
    # the same number of elements we can unpack them into
    # four separate variables - this is more readable than 
    # referencing by index.
    for first_name, last_name, hours, pay_rate in reader:

        employeeRegularHours, employeeOvertimeHours = calculateRegularHours(hours)
        employeeOvertimeHours = calculateOvertimeHours(hours)
        employeeTotalHours = calculateTotalHours(employeeRegularHours, employeeOvertimeHours)

        # Do more stuff

请注意,从文件中读取时hourspay_rate字符串 如果要将它们用作数字,则需要使用intfloat转换它们,具体取决于您想要的数字类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM