You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ydbf库转换DBF到CSV时遇ValueError:无法解析b'RE'值

DBF转CSV时遇到ValueError:无效的整数字面值b'RE'

问题描述

我需要将598个DBF文件转换为CSV格式,使用Python的ydbf库编写了如下转换脚本:

with ydbf.open(filename, encoding=encoding) as dbf:
   for record in dbf:
      records[index] = record
      index += 1
   df = pd.DataFrame.from_dict(records, orient='index')
df.to_csv(filename[:-4] + ".csv", index=False)

其中6个文件转换出错,5个已解决,但最后一个始终抛出以下错误:

Error occured (ValueError: invalid literal for int() with base 10: b'RE') while reading rec #0.

我尝试过latin、cp1250、cp1251、cp1252及ascii等多种编码方式,也试过用另一段代码:

dbf = DBF(file)
dataResult = pd.DataFrame(iter(dbf))

问题依旧。同时我还写了自定义异常处理代码,试图跳过错误值,但还是没解决:

try:
    df = pd.DataFrame()
    for file in dbf3_file_errors['filename']:
        with ydbf.open(file, encoding="ascii") as dbf:
            for record in dbf:
                updated_record = []
                for value in record:
                    try:
                        float_value = float(value)
                        updated_record.append(float_value)
                    except ValueError:
                        try:
                            int_value = int(value)
                            updated_record.append(int_value)
                        except ValueError:
                            try:
                                str_value = str(value)
                                updated_record.append(str_value)
                            except:
                                if value == 'RE':
                                    updated_record.append(np.nan)
                df = df.append(pd.DataFrame([updated_record]), ignore_index=True)
                break
    df.to_csv(file[:-4] + ".csv", index=False)
except Exception as e:
    print("Error occured:", e)

以下是这个问题DBF文件的样本内容:

*  Á '                    ALUPROF    C                  XB         N
                  YB         N                    LAAG       N                  VOLGNR     N!                   
 EK_BDA20-114   3.0000  20.0000RE   100 EK_BDA20-114   1.5000  19.5981RE   200 EK_BDA20-114   0.4019  18.5000RE   300 EK_BDA20-114   0.0000  17.0000RE   400 EK_BDA20-114   0.0000   0.0000RE   500 EK_BDA20-114 114.0000   0.0000RE   600 EK_BDA20-114 114.0000  17.0000RE   700 EK_BDA20-114 113.5981  18.5000RE   800 EK_BDA20-114 112.5000  19.5981RE 

内容的提问来源于stack exchange,提问作者Luuk_148

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 14:25:21