我正在尝试与加拿大广播电台DBF文件在这里公开:00015.html
我想将fmstatio.dbf文件专门读到Pandas DataFrame中。我已经在Python中试用了两个常用的DBF包。
当使用simpledbf (https://pypi.org/project/simpledbf/)时,我只在使用dbf.to_dataframe()函数时才得到列名。
我还在pypi (https://pypi.org/project/dbf/)上尝试了dbf。我能够将DBF文件读入表中:
table = dbf.Table(filename='/datadrive/canada/fmstatio.dbf')
table.open(dbf.READ_ONLY)
print(table)
table.close()
并在表中获取以下信息:
Table: /datadrive/canada/fmstatio.dbf
Type: dBase III Plus
Codepage: ascii (plain ol' ascii)
Status: DbfStatus.READ_ONLY
Last updated: 1921-12-07
Record count: 8428
Field count: 37
Record length: 221
但是,当我试图转换为DataFrame时,我没有成功:
oh_canada = pd.DataFrame(table)
table.close()
收到错误:
data = fielddef[CLASS](decoder(data)[0])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 4: ordinal not in range(128)
有人能洞察到在Pandas中使用这种DBF文件的最佳方法吗?在此之前,非常感谢您。
发布于 2021-12-13 02:48:25
桌子上写着它是“朴素的老ascii",但它是存在的。它包含"e和尖锐的口音“,这是不足为奇的法语内容在加拿大的数据库。要解决这个问题,您需要重写代码页:
table = dbf.Table(filename='/datadrive/canada/fmstatio.dbf',codepage=3)
"3“是指默认的Windows代码页,CP1252。有了这个,我就能读到文件了。
我仍然不确定pandas
能否导入它作为迭代器提供的格式。您可能需要使用export
将其转换为列表。
https://stackoverflow.com/questions/70329295
复制相似问题