简体   繁体   English

ValueError:int() 的无效文字,基数为 10:'FALSE' 从数据集中删除空字符串时

[英]ValueError: invalid literal for int() with base 10: 'FALSE' When removing empty string from a dataset

with open(filename,'r') as input_file:
    
csv_reader = csv.reader(input_file,delimiter = ',')
    
    for line_number, line in enumerate(csv_reader):
        if line_number == 0: # skip the header
            continue
        #if line[10] == '':
            #line.insert(10,0)
        my_dic.append({
            
            'First Name':line[11],
            'Last name':line[13],
            'Age(Years)':int(line[3]),
            'Sex':line[18],
            'type of car':line[16],
            'Marital Status':line[14],
            'Dependants':line[10],
            'Yearly Salary':int(line[17]),
            'Yearly Pension':int(line[15]),
            'Company':line[5],
            'Commuted Distance':float(line[4]),
            'vehicle':{
                'Make':line[19],
                'model':line[20],
                'year':int(line[21]),
                'category':line[22]
                
            },
            'Credit Card':{
                'Start Date':line[6],
                'End Date':line[7],
                'Card number':line[8],
                'Card CCV':int(line[9]),
                'iban':line[12]
                
            },
            'Address':{
                'Street':line[0],
                'City':line[1],
                'Postcode':line[2]
            }
            
            
            
        })

I have the code above converting csv file to dictionary, and I also want to replace the empty strings in column 10 of the dataset.我有上面将 csv 文件转换为字典的代码,我还想替换数据集第 10 列中的空字符串。 If I remove the commented code (the code that tries to replace the empty string in column 10 (of the data) with a number, my code works. However, if I remove the comment, it gives value error at 'yearly salary' key that I typecast to integers)如果我删除注释代码(尝试用数字替换第 10 列(数据)中的空字符串的代码,我的代码有效。但是,如果我删除注释,它会在“年薪”键处给出值错误我将类型转换为整数)

1.How else can I replace empty string in the column with a number (I don't want to use pandas) 1.如何用数字替换列中的空字符串(我不想使用pandas)

  1. I also want know the rows where the correction takes place我也想知道发生更正的行

line.insert(10,0) inserts additional value to the array. line.insert(10,0)向数组插入附加值。 For example, if the array was length 20, after insert , it will be of length 21. You get wrong values, and on position 17, you get a non-integer value.例如,如果数组长度为 20,则在insert之后,长度为 21。您得到错误的值,而在位置 17 上,您得到一个非整数值。

You want to replace the value, not insert it.您想替换该值,而不是插入它。

line[10] = 0

However, it would be easier to understand the code if the conversion is done in the dictionary itself.但是,如果转换是在字典本身中完成的,那么理解代码会更容易。

{
...
"Dependants": line[10] or 0,
...
}
  1. To get the line number, the easiest way is to store line_number to some array... For example,要获取行号,最简单的方法是将line_number存储到某个数组中...例如,
missing_data_on_dependants_row_idxs = list()
for line_number, line in enumerate(csv_reader):
    if line_number == 0: # skip the header
        continue
    if line[10] is None:
        missind_data_on_dependants_row_idxs.append(line_number)
...

Also, referring to the columns by indexes is hard to understand.此外,通过索引引用列也很难理解。 Did you consider using csv.DictReader ?你考虑过使用csv.DictReader吗?

暂无
暂无

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

相关问题 ValueError:int() 的无效文字,基数为 10:'string' - ValueError: invalid literal for int() with base 10: 'string' 将字符串从 txt 文件转换为 integer 时,出现 ValueError: invalid literal for int() with base 10: - When converting string to integer from txt file, I get ValueError: invalid literal for int() with base 10: ValueError:对于带有基数为10的int()的无效文字:'' - 字符串到int转换 - ValueError: invalid literal for int() with base 10: '' - string to int conversion ValueError:在 Python 中尝试将字符串转换为 Int 时,出现基数为 10 的 int() 的无效文字 - ValueError: invalid literal for int() with base 10 Shows up When Trying to Convert String into Int in Python ValueError:以10为底的int()的无效文字:''发送空值时出现此错误 - ValueError: invalid literal for int() with base 10: '' I got this error when empty value was sent ValueError:int() 的无效文字,基数为 10:'SOME STRING' - ValueError: invalid literal for int() with base 10: 'SOME STRING' ValueError:int() 的无效文字,我的字符串以 10 为底 - ValueError: invalid literal for int() with base 10 for my string ValueError:以 10 为基数的 int() 的无效文字:对于任何字符串 - ValueError: invalid literal for int() with base 10: for any string ValueError:int() 的无效文字,基数为 10: ' ' 之前工作时 - ValueError: invalid literal for int() with base 10: ' ' when it worked before ValueError: int() 的无效文字,以 10 为基数:运行 unittest 时为“30.0” - ValueError: invalid literal for int() with base 10: '30.0' when running unittest
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM