![](/img/trans.png)
[英]How can I solve ValueError in django. Exception Value: ModelForm has no model class specified
[英]How Can I Solve ValueError dtype Without Using An Exception
我正在开发一个程序,该程序应该根据年龄将员工工资更新 %5 和 %10:
import csv
infile = open('employee.csv')
csvreader = csv.reader(infile)
rows = []
for row in csvreader:
rows.append(row)
for i in rows:
if(int(i[2]) < 40): #LINE CAUSING A PROBLEM
i[3] = round((1.05 * float(i[3])) , 2)
else:
i[3] = round((1.10 * float(i[3])) , 2)
print('\n\nList after updation:')
#loop print the data on the compile
for row in rows:
print(row)
#open file and write the updated data
with open('employeeUpdate.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
for row in rows:
writer.writerow(row)
当我运行它时,我收到以下错误:
ValueError Traceback (most recent call last)
---> 23 if(int(i[2]) < 40):
ValueError: invalid literal for int() with base 10: 'age'
数据样本:
ID employee name age salary
1 Sara Wales 33 60994
2 John Smith 42 78399
3 Michael Ousley 22 58000
4 Rami Elliot 50 88382
我仔细检查了数据类型,它是一个整数--> ('age', dtype('int64'))
我尝试with open ('employee.csv', r) as infile
并将问题行更改为if int(float(i[2]) < 40):
但它们都不起作用。 它说不能将字符串转换为浮点数。 我不知道为什么它将整数作为字符串读取。
但是当我添加这样的异常时:
for i in rows:
try:
if (int(i[2]) < 40):
i[3] = round((1.05 * int(i[3])) , 2)
else:
i[3] = round((1.10 * int(i[3])) , 2)
except ValueError:
print("")
它起作用了,所以我的问题是为什么它只在异常情况下起作用!,有没有办法让它在没有异常的情况下完成?
由于csv.reader()
连续读取流直到 EOF,因此它没有标题行的概念。 对于rows
的第一次迭代, i
将始终是字符串标题行。 并且您正在尝试将文本“age”转换为 int,这会使 Python 出错。
您的 try-except 有效,因为它只是掩盖了从第一行引发的错误并打印了一个空行。
要修复它,只需从文件中跳过一行以不包含标题行,或者在进行 int 转换时跳过第一次迭代。
with open('employee.csv') as infile:
infile.readline()
csvreader = csv.reader(infile)
# do stuff with csvreader
在处理大型数据集和进行复杂的数据操作时,请考虑使用 pandas 库。 此处描述的问题和 dtype 转换将由 Pandas 自动处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.