如何在python中读取dbf文件并将其转换为dataframe

问题描述 投票:0回答:2

我正在尝试使用

simpledbf
库读取 dbf 文件并转换为数据帧以进行进一步处理。

from simpledbf import Dbf5
dbf = Dbf5(r"C:\Users\Prashant.kumar\Downloads\dbf\F1_1.DBF")
df1 = dbf.to_dataframe()

不幸的是,我收到以下错误。

我试图找到解决方案,但找不到解决方案,也找不到将 dbf 文件转换为数据帧以进行后处理的替代方法。

这是文件 https://mega.nz/folder/gKIBUKIa#rE7TmE5FToLzCblMhLLFbw

有没有办法将此 dbf 作为数据帧读取到 python 中?

python pandas dbf
2个回答
1
投票

使用

dbfread
代替
simpledbf
:

# pip install dbfread
from dbfread import DBF
from pandas import DataFrame

dbf = DBF('F1_1.DBF')
df = DataFrame(iter(dbf))

输出:

>>> df
     RESPONDENT                                         RESPONDEN2 RESPONDEN3 STATUS  FORM_TYPE  STATUS_DAT SORT_NAME PSWD_GEN _NullFlags
0             1                             AEP Generating Company                 A          0  1990-01-01                       b'\x00'
1             2                              ALABAMA POWER COMPANY                 A          0  2000-05-03                       b'\x00'
2             3            Alaska Electric Light and Power Company                 A          0  1990-01-01                       b'\x00'
3             4                        Alcoa Power Generating Inc.                 A          0  1990-01-01                       b'\x00'
4             5                   THE ALLEGHENY GENERATING COMPANY                 A          0  1990-01-01                       b'\x00'
..          ...                                                ...        ...    ...        ...         ...       ...      ...        ...
389         538                                    DesertLink, LLC                 A         -1  2020-11-17                       b'\x00'
390         539  NextEra Energy Transmission MidAtlantic Indian...                 A         -1  2020-12-03                       b'\x00'
391         540                      Wilderness Line Holdings, LLC                 A         -1  2020-12-15                       b'\x00'
392         541                McKenzie Electric Cooperative, Inc.                 A         -1  2021-04-19                       b'\x00'
393         542               LS Power Grid New York Corporation I                 A          0  2021-08-27                       b'\x00'

[394 rows x 9 columns]

0
投票

dbfread 给出此错误返回decode_text(text,self.encoding,errors = self.char_decode_errors) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ UnicodeDecodeError:“ascii”编解码器无法解码位置 11 中的字节 0xac:序数不在范围内(128)

我建议采用不同的方法。

© www.soinside.com 2019 - 2024. All rights reserved.