【发布时间】:2019-07-26 08:01:48
【问题描述】:
我正在尝试打开将几个 DBF 文件转换为数据框。他们中的大多数工作正常,但对于其中一个文件,我收到错误: "UnicodeDecodeError: 'utf-8' 编解码器无法解码位置 15 中的字节 0xf6: 无效起始字节"
我在其他一些主题上阅读过此错误,例如打开 csv 和 xlsx 以及其他文件。建议的解决方案是包含encoding = 'utf-8'
在阅读文件部分。不幸的是,我还没有找到 DBF 文件的解决方案,而且我对 DBF 文件的了解非常有限。
到目前为止我所尝试的:
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'
最后,如果我尝试安装 dbfpy 模块,我会收到:
SyntaxError: 无效语法
关于如何解决这个问题的任何建议?
【问题讨论】:
-
可能是你的python版本(选项3)。