[英]Struggles with converting a DBF file to Pandas DataFrame
我正在嘗試使用在此處公開的加拿大廣播電台 DBF 文件: https://sms-sgs.ic.gc.ca/eic/site/sms-sgs-prod.nsf/eng/h_00015.html
我想專門將 fmstatio.dbf 文件讀入 Pandas DataFrame。 我試過 Python 中兩個常用的 DBF 包。
使用 simpledbf ( https://pypi.org/project/simpledbf/ ) 時,我只在使用 dbf.to_dataframe() function 時獲得列名。
我還在 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)
可能有人對 go 關於在 Pandas 中使用這種 DBF 文件的最佳方法有見解嗎? 提前謝謝了。
該表說它是“普通的舊 ascii”,但它是謊言。 它包含“帶有尖銳口音的 e”,考慮到加拿大數據庫中的法語內容,這並不奇怪。 要解決此問題,您需要覆蓋代碼頁:
table = dbf.Table(filename='/datadrive/canada/fmstatio.dbf',codepage=3)
“3”表示默認的 Windows 代碼頁 CP1252。 這樣,我就可以讀取文件了。
我仍然不確定pandas
可以導入它作為迭代器提供的格式。 您可能需要使用export
將其轉換為列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.