1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Problems opening DBF files in python

Discussão em 'Python' iniciado por Stack, Setembro 27, 2024 às 17:02.

  1. Stack

    Stack Membro Participativo

    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' codec can't decode byte 0xf6 in position 15: invalid start byte"

    I have read this error on some other topics such as opening csv and xlsx and other files. The proposed solution was to include encoding = 'utf-8' in the reading the file part. I haven't found a solution for DBF files unfortunately and I have very limited knowledge on DBF files.

    What I have tried so far:

    1)

    from dbfread import DBF
    dbf = DBF('file.DBF')
    dbf = pd.DataFrame(dbf)

    UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 8: character maps to <undefined>


    2)

    from simpledbf import Dbf5
    dbf = Dbf5('file.DBF')
    dbf = dbf.to_dataframe()

    UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf6 in position 15: invalid start byte


    3)

    # 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'


    And last, if I try to install the dbfpy module, I receive: SyntaxError: invalid syntax

    Any suggestions on how to solve this?

    Continue reading...

Compartilhe esta Página