[英]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)
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,
...
}
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.