Problems opening DBF files in python

前端 未结 2 2019
礼貌的吻别
礼貌的吻别 2021-01-14 05:49

I am trying to open en transform several DBF files to a dataframe. Most of them worked fine, but for one of the files I receive the error: \"UnicodeDecodeError: \'utf-8\' co

相关标签:
2条回答
  • 2021-01-14 06:29

    Try using my dbf library:

    import dbf
    
    table = dbf.Table('file.DBF')
    

    Print it to see if an encoding is present in the file:

    print table    # print(table) in Python 3
    

    One of my test tables looks like this:

        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
    

    The important line being the Codepage line -- it sounds like that is not properly set for your DBF file. If you know what it should be, you can either open it with that codepage (temporarily) with:

    table = dbf.Table('file.DBF', codepage='...')
    

    Or you can change it permanently (updates the DBF file) with:

    table.open()
    table.codepage = dbf.CodePage('cp1252') # for example
    table.close()
    
    0 讨论(0)
  • 2021-01-14 06:43
     from simpledbf import Dbf5
     dbf2 = Dbf5('/Users/.../TCAT_MUNICIPIOS.dbf', codec='latin')
     df2 = dbf2.to_dataframe()
     df2.head(3)
    
    0 讨论(0)
提交回复
热议问题