简体   繁体   English

如何在不使用异常的情况下解决 ValueError dtype

[英]How Can I Solve ValueError dtype Without Using An Exception

I am working on a program that is supposed to update employees salaries by %5 and %10 based on age:我正在开发一个程序,该程序应该根据年龄将员工工资更新 %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) 

When I run it I get the following error:当我运行它时,我收到以下错误:

ValueError                                Traceback (most recent call last)
---> 23   if(int(i[2]) < 40):
ValueError: invalid literal for int() with base 10: 'age'

Data Sample:数据样本:

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

I double-checked the data type and it was an integer--> ('age', dtype('int64'))我仔细检查了数据类型,它是一个整数--> ('age', dtype('int64'))

I tried with open ('employee.csv', r) as infile and changing the problem line to if int(float(i[2]) < 40): but they both did not work.我尝试with open ('employee.csv', r) as infile并将问题行更改为if int(float(i[2]) < 40):但它们都不起作用。 It said cannot convert string to float.它说不能将字符串转换为浮点数。 I don't know why it is reading the integer as a string.我不知道为什么它将整数作为字符串读取。

But when I added an exception like this:但是当我添加这样的异常时:

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("")

It worked, so my question is why did it only work with the exception!, and is there a way I can have it done without the exception?它起作用了,所以我的问题是为什么它只在异常情况下起作用!,有没有办法让它在没有异常的情况下完成?

Since csv.reader() continuously reads a stream until EOF, it doesn't have a concept of a header row.由于csv.reader()连续读取流直到 EOF,因此它没有标题行的概念。 For the first iteration of rows , i would always be the string header row.对于rows的第一次迭代, i将始终是字符串标题行。 And you're trying to convert the text "age" into int which would Python to error.并且您正在尝试将文本“age”转换为 int,这会使 Python 出错。

Your try-except works because it simply masks the error raised from the first row and prints a blank line instead.您的 try-except 有效,因为它只是掩盖了从第一行引发的错误并打印了一个空行。

To fix it, simply skip a line from the file to not include the header row, or skip the first iteration when doing the int conversion.要修复它,只需从文件中跳过一行以不包含标题行,或者在进行 int 转换时跳过第一次迭代。

with open('employee.csv') as infile:
    infile.readline()
    csvreader = csv.reader(infile)
    # do stuff with csvreader

When handling large datasets and doing complicated data manipulations, consider using the pandas library.在处理大型数据集和进行复杂的数据操作时,请考虑使用 pandas 库。 The issue described here and dtype conversions would automatically be handled by pandas.此处描述的问题和 dtype 转换将由 Pandas 自动处理。

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

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