[英]Where is this extra line come from when printing an item from a set? (CSV) file Using Python and Spyder4
I am confounded by my print(tmp[3]) function of a item, in a set.我对一组中的一个项目的 print(tmp[3]) function 感到困惑。 When the item tmp[3] is printed it gives me two lines one with the value and the other empty.
打印项目 tmp[3] 时,它给了我两行,其中一行是值,另一行是空的。 Everything runs well up to line 132.
一切运行良好,直到第 132 行。
The goal of the program is to print a The payroll summary report is based on information from previous programs (with different rates).该程序的目标是打印一份工资汇总报告,该报告基于以前程序的信息(具有不同的费率)。 You should use individual functions to perform the following:
您应该使用单独的函数来执行以下操作:
Compute the regular hours worked (40 hours and below) Compute the overtime hours worked (those above 40 hours) Compute the regular pay (regular hours times regular pay) Compute the overtime pay (overtime hours times regular pay times 1.5) Compute the gross pay Compute the amount of federal tax withheld (15.1% of gross pay) Compute the amount of state tax withheld (5.5% of gross pay) Compute the amount of medicare withheld (1.2% of gross pay) Compute the amount of social security withheld (4.8% of gross pay) Compute the total deductions (federal tax + state tax + medicare + social security) Compute the net pay (gross pay - deductions) Compute the total (total for everyone) net pay Print (to the screen) a clean summary report (see the output above) Print (to the screen) the total net pay.计算正常工作时间(40 小时及以下) 计算加班时间(40 小时以上) 计算正常工资(正常时间乘以正常工资) 计算加班工资(加班时间乘以正常工资乘以 1.5) 计算总工资计算预扣的联邦税金额(总工资的 15.1%) 计算预扣的 state 税的金额(总工资的 5.5%) 计算预扣的医疗保险金额(总工资的 1.2%) 计算预扣的社会保障金额(4.8总工资的百分比)计算总扣除额(联邦税 + state 税 + 医疗保险 + 社会保障)计算净工资(总工资 - 扣除)计算总工资(每个人的总和)净工资打印(到屏幕上)一个干净的摘要报告(见上面的 output)打印(到屏幕上)总净支付。
I found the functions for an input based version so it was a challenge for me to extract data from a csv and use it in the functions.我找到了基于输入的版本的函数,因此从 csv 中提取数据并在函数中使用它对我来说是一个挑战。
IF you want an easier copy and past program similar to this one here is the link: Read records from CSV file and print report如果你想要一个更简单的复制和过去的程序类似于这个链接: 从 CSV 文件中读取记录并打印报告
The expected result of print(tmp[3]) is "10". print(tmp[3]) 的预期结果是“10”。 Actual result.. "10 ".
实际结果..“10”。 I don't know why it prints an extra line.
我不知道为什么它会打印额外的一行。 THe symptom of this problem i thinkis not being able to calculate the regular pay = Payrate * hoursWorked.
我认为这个问题的症状是无法计算正常工资 = Payrate * hoursWorked。 The expect output is 420. 42(payrate) * 10(hours) = 420 Here is the error at line 133 "TypeError:can't multiply sequence by non-int of type 'float'" at line 132 This is a homework assignment and I'm just looking to solve this error, so putting my whole program seems unnecessary.
期望 output 为 420。 42(payrate) * 10(hours) = 420 这是第 133 行的错误“TypeError:can't multiply sequence by non-int of type 'float'” at line 132 这是一个家庭作业我只是想解决这个错误,所以我的整个程序似乎没有必要。
Here is the csv file:这是 csv 文件:
First, Last, Hours, Pay
Matthew, Hightower, 42, 10
Samuel, Jackson, 53, 12.58
Catherine, Jones, 35, 19.43
THis is the whole program.这是整个程序。 def main():
定义主():
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 # you can zip range() if you want to with open(fname, 'r') as f: 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"]))
main()主要的()
[Expected Output][1]
Payroll Summary Report
Last Name First Name Hours Regular Hours Overtime Hours姓 名 工作时间 正常工作时间 加班时间
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 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
Total Net Pay 1,398.90总净工资 1,398.90
Regular Pay OT Pay Gross Pay Deductions Net Pay定期工资 OT 工资 总工资 扣除额 净工资
The output of f.readlines
includes end of line (EOL) characters, so if you split the line on commas and print the last item it will print the value and the end of line character(s). f.readlines 的
f.readlines
包括行尾 (EOL) 字符,因此如果您以逗号分隔行并打印最后一项,它将打印值和行尾字符。 If you do print(repr(tmp[3]))
you will see something like '10\n'
.如果你做
print(repr(tmp[3]))
你会看到类似'10\n'
东西。
You can strip the EOL from the line like this:您可以像这样从行中删除 EOL:
tmp = line.strip().split(",")
However, since you are processing a csv file you can use Python's built in csv module, which will handle some of the details for you但是,由于您正在处理 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
Note that hours
and pay_rate
are strings when read from the file.请注意,从文件中读取时
hours
和pay_rate
是字符串。 If you want to use them as numbers you will need to convert them using int or float , depending on what kind of numbers you want.如果要将它们用作数字,则需要使用int或float转换它们,具体取决于您想要的数字类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.