在python中打开DBF文件时出现问题

在python中打开DBF文件时出现问题,python,dbf,Python,Dbf,我试图打开并将几个DBF文件转换为一个数据帧。其中大多数工作正常,但对于其中一个文件,我收到了错误: “UnicodeDecodeError:'utf-8'编解码器无法解码位置15中的字节0xf6:无效的开始字节” 我在其他一些主题上读过这个错误,比如打开csv和xlsx以及其他文件。建议的解决方案包括编码='utf-8' 在读取文件部分。不幸的是,我还没有找到DBF文件的解决方案,而且我对DBF文件的了解非常有限 到目前为止,我所尝试的: (一) (三) 最后,如果我尝试安装dbfpy模块,我

我试图打开并将几个DBF文件转换为一个数据帧。其中大多数工作正常,但对于其中一个文件,我收到了错误: “UnicodeDecodeError:'utf-8'编解码器无法解码位置15中的字节0xf6:无效的开始字节”

我在其他一些主题上读过这个错误,比如打开csv和xlsx以及其他文件。建议的解决方案包括
编码='utf-8'
在读取文件部分。不幸的是,我还没有找到DBF文件的解决方案,而且我对DBF文件的了解非常有限

到目前为止,我所尝试的:

(一)

(三)

最后,如果我尝试安装
dbfpy
模块,我会收到: SyntaxError:无效语法

关于如何解决这个问题有什么建议吗?

尝试使用:

打印以查看文件中是否存在编码:

print table    # print(table) in Python 3
我的一个测试表如下所示:

    Table:         tempy.dbf
    Type:          dBase III Plus
    Codepage:      ascii (plain ol ascii)
    Status:        DbfStatus.CLOSED
    Last updated:  2019-07-26
    Record count:  1
    Field count:   2
    Record length: 31 
    --Fields--
      0) name C(20)
      1) desc M
重要的一行是
Codepage
行——听起来好像没有为
DBF
文件正确设置这一行。如果您知道它应该是什么,您可以使用以下代码页(临时)打开它:

或者,您可以使用以下命令永久更改它(更新
DBF
文件):

  • 安装库DBF

    conda安装DBF

  • 从dbfread导入DBF

  • db\u in\u dbf=dbf('paht/database.dbf)
    此行上传数据库

  • df=pd.DataFrame(db_in_dbf)
    此行转换数据帧


  • 它可能是您的python版本(选项3)。我已经尝试了
    dbf.Table('file.dbf')
    ,但我得到了错误:
    TypeError:预期的字符串或类似对象的字节数
    @DmitryKuznetsov:请发布一个新问题并包括回溯。simpledbf仅在3.6=# this block of code copied from https://gist.github.com/ryan-hill/f90b1c68f60d12baea81 import pysal as ps def dbf2DF(dbfile, upper=True): #Reads in DBF files and returns Pandas DF db = ps.table(dbfile) #Pysal to open DBF d = {col: db.by_col(col) for col in db.header} #Convert dbf to dictionary #pandasDF = pd.DataFrame(db[:]) #Convert to Pandas DF pandasDF = pd.DataFrame(d) #Convert to Pandas DF if upper == True: #Make columns uppercase if wanted pandasDF.columns = map(str.upper, db.header) db.close() return pandasDF dfb = dbf2DF('file.DBF') AttributeError: module 'pysal' has no attribute 'open'
    import dbf
    
    table = dbf.Table('file.DBF')
    
    print table    # print(table) in Python 3
    
        Table:         tempy.dbf
        Type:          dBase III Plus
        Codepage:      ascii (plain ol ascii)
        Status:        DbfStatus.CLOSED
        Last updated:  2019-07-26
        Record count:  1
        Field count:   2
        Record length: 31 
        --Fields--
          0) name C(20)
          1) desc M
    
    table = dbf.Table('file.DBF', codepage='...')
    
    table.open()
    table.codepage = dbf.CodePage('cp1252') # for example
    table.close()
    
     from simpledbf import Dbf5
     dbf2 = Dbf5('/Users/.../TCAT_MUNICIPIOS.dbf', codec='latin')
     df2 = dbf2.to_dataframe()
     df2.head(3)