[英]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
請注意,從文件中讀取時hours
和pay_rate
是字符串。 如果要將它們用作數字,則需要使用int或float轉換它們,具體取決於您想要的數字類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.