《Python数据处理》9.1.1导入数据笔记:agate.exceptions.CastError: Can not parse value as Decimal. Error at row

《Python数据处理》9.1.1导入数据笔记:agate.exceptions.CastError: Can not parse value as Decimal. Error at row

成功去掉报错后的截图
在这里插入图片描述

一、报错现象:

源码:

def agate_data_check(self):
    '''数据类型猜测,将xlrd数据类型转为agate数据类型'''
    text_type = agate.Text()
    number_type = agate.Number()
    boolean_type = agate.Boolean()
    date_type = agate.DataType()

    example_row = self.xlrd_read_xls().row(6)
    #示例
    print(example_row)
    print(example_row[0].ctype)#类型
    print(example_row[0].value)
    print(ctype_text)#这应当是一个固定的设置的字典
    '''xlrd的数据类型'''

    '''将xlrd的数据类型转为agate的数据类型'''
    types = []
    for v in example_row:
        '''ctype_text为字典'''
        value_type = ctype_text[v.ctype]
        #一个一个
        #print(value_type, end=' ')
        if value_type == 'text':
            types.append(text_type)
        elif value_type == 'number':
            types.append(number_type)
        elif value_type =='xldate':
            types.append(date_type)
        else:
            types.append(text_type)
    for type in types:
        pass
    #return types
        #pprint.pprint(type)
    '''导入agate表中'''
    table = agate.Table(self.country_data(), self.combine_title(), types)#self.agate_data_check())

在此处进了函数封装,基本内容与书上源码一致,或许存在变量名有不一致的地方

运行后出现报错

agate.exceptions.CastError: Can not parse value "" as Decimal. Error at row 1 column Place of residence (%) Urban.

而没有出现书中所述的

CastError can not convert value '-' to Decimal for NumberColumn

即使进行清理

 def get_new_array(self, clean_function):
    new_arr = []
    for row in self.country_data():
        clean_row = [clean_function(rv) for rv in row]
        new_arr.append(clean_row)
    pprint.pprint(new_arr)
    table = agate.Table(new_arr, self.combine_title(), self.agate_data_check()
    #注:同上进行过封装,所以titles变成了self.combine_title()

一样的报错

在这里插入图片描述

二、解决方法

由于与书中错误不同,所以自行解决问题

第一步:

最后一个报错为

agate.exceptions.CastError: Can not parse value "" as Decimal. Error at row 1 column Place of residence (%) Urban.

位于

File "D:\Users\Administrator\AppData\Local\Programs\Python\Python37\lib\site-packages\agate\table\__init__.py", line 133, in __init__
raise CastError(str(e) + ' Error at row %s column %s.' % (i, self._column_names[j]))

找到这个文件,将except内容注释掉
在这里插入图片描述
结果成功

在这里插入图片描述
在这里插入图片描述

注意:如果第一步未成功则进行第二步:

第二步:

首先,
(ps:在第一次如同第一步修改时,出现一样的报错即修改未成功,但是第二次只进行第一步时的修改时成功了,可能会需要先进行第二步)

agate.exceptions.CastError: Can not parse value "" as Decimal. Error at row

然后向上看上一个报错:

agate.exceptions.CastError: Can not parse value "" as Decimal.

位于
在这里插入图片描述
注释掉
在这里插入图片描述
结果contries and area为空。
在这里插入图片描述

其次,去掉注释进行还原

在这里插入图片描述

最后,回到第一步的修改方式

在这里插入图片描述

运行结果
在这里插入图片描述

三、疑惑

不知道为什么,其修改后成功的原理不明

原创文章 62 获赞 29 访问量 7万+

猜你喜欢

转载自blog.csdn.net/python__reported/article/details/106155610