TypeError: Object of type ‘int64’ is not JSON serializable

train_uy = pd.read_csv('./src_data/train_10000_uy.csv', sep='\t',
                           header=None, names=['uid', 'content', 'content_type', 'entity'])
for i in range(len(c)):
            # if c[2][0] != 'NaN' and c[3][0] != 'NaN':
            # 清洗数据
            uid = c['uid'][i]
            uid = int(uid)
            text = c['content'][i]
            # 抽取实体位置
            
            entity = c['entity'][i]
            content_type = c['content_type'][i]
            # print("entity: ", idx, type(entity), entity)
            for loc in list(findall(p=entity, s=text)):
                loc1 = loc
            
            result.append({
                'id': uid,
                'context': text,
                'type': content_type,
                # 每个类别对应一个实体列表,每个实体对应一个位置列表,文本中的实体很多所以位置很多
                'entity': [[loc1]]
            })
        # shuffle
        random.seed(2020)
        random.shuffle(result)

        print(f'get {len(result)} train samples.')
        json.dump(result, f_train, indent=4, ensure_ascii=False)
        print(f'get {len(result[-900:])} val samples.')
        json.dump(result[-900:], f_val, indent=4, ensure_ascii=False)

这里代码的话就是使用pandas读取数据后,再转入json数据格式时,uid的类型要转化为int类型才行。

uid = int(uid)

以此解决问题。

参考链接:https://jimolonely.github.io/2018/04/17/python/008-python-error/

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值