簡體   English   中英

在 python 中將 dbf 轉換為 csv 的方法?

[英]Way to convert dbf to csv in python?

我有一個包含一堆 dbf 文件的文件夾,我想將其轉換為 csv。我嘗試使用代碼將擴展名從 .dbf 更改為 .csv,當我使用 Excel 時,這些文件可以正常打開,但是當我打開他們在 pandas 他們看起來像這樣:

                                                s\t�
0                                                NaN
1            1       176 1.58400000000e+005-3.385...

這不是我想要的,那些字符不會出現在真實文件中。
我應該如何正確讀取 dbf 文件?

使用我的dbf庫,您可以執行以下操作:

import sys
import dbf
for arg in sys.argv[1:]:
    dbf.export(arg)

這將創建一個與每個dbf文件同名的.csv文件。 如果將該代碼放入名為dbf2csv.py的腳本中,則可以將其稱為

python dbf2csv.py dbfname dbf2name dbf3name ...

在線查看,有以下幾種選擇:


使用simpledbf

dbf = Dbf5('fake_file_name.dbf')
df = dbf.to_dataframe()

從要點調整:

import pysal as ps

def dbf2DF(dbfile, upper=True):
    "Read dbf file and return pandas DataFrame"
    with ps.open(dbfile) as db:  # I suspect just using open will work too
        df = pd.DataFrame({col: db.by_col(col) for col in db.header})
        if upper == True: 
           df.columns = map(str.upper, db.header) 
        return df

編輯#2:

可以使用dbfread讀取dbf文件,而無需轉換為csv(只需使用pip install dbfread ):

>>> from dbfread import DBF
>>> for row in DBF('southamerica_adm0.dbf'):
...     print row
... 
OrderedDict([(u'COUNTRY', u'ARGENTINA')])
OrderedDict([(u'COUNTRY', u'BOLIVIA')])
OrderedDict([(u'COUNTRY', u'BRASIL')])
OrderedDict([(u'COUNTRY', u'CHILE')])
OrderedDict([(u'COUNTRY', u'COLOMBIA')])
OrderedDict([(u'COUNTRY', u'ECUADOR')])
OrderedDict([(u'COUNTRY', u'GUYANA')])
OrderedDict([(u'COUNTRY', u'GUYANE')])
OrderedDict([(u'COUNTRY', u'PARAGUAY')])
OrderedDict([(u'COUNTRY', u'PERU')])
OrderedDict([(u'COUNTRY', u'SURINAME')])
OrderedDict([(u'COUNTRY', u'U.K.')])
OrderedDict([(u'COUNTRY', u'URUGUAY')])
OrderedDict([(u'COUNTRY', u'VENEZUELA')])

我更新的參考資料

官方項目網站: http//pandas.pydata.org

官方文件: http//pandas-docs.github.io/pandas-docs-travis/

dbfreadhttpsdbfread

geopandashttpgeopandas

帶有geopandas shp和dbfhttpsgeopandas

這是我多年來一直使用的解決方案。 我有一個Python 2.7的解決方案和一個Python 3.5的解決方案(可能還有3.6)。

Python 2.7:

import csv
from dbfpy import dbf

def dbf_to_csv(out_table):#Input a dbf, output a csv
    csv_fn = out_table[:-4]+ ".csv" #Set the table as .csv format
    with open(csv_fn,'wb') as csvfile: #Create a csv file and write contents from dbf
        in_db = dbf.Dbf(out_table)
        out_csv = csv.writer(csvfile)
        names = []
        for field in in_db.header.fields: #Write headers
            names.append(field.name)
        out_csv.writerow(names)
        for rec in in_db: #Write records
            out_csv.writerow(rec.fieldData)
        in_db.close()
    return csv_fn

Python 3.5:

import csv
from dbfread import DBF

def dbf_to_csv(dbf_table_pth):#Input a dbf, output a csv, same name, same path, except extension
    csv_fn = dbf_table_pth[:-4]+ ".csv" #Set the csv file name
    table = DBF(dbf_table_pth)# table variable is a DBF object
    with open(csv_fn, 'w', newline = '') as f:# create a csv file, fill it with dbf content
        writer = csv.writer(f)
        writer.writerow(table.field_names)# write the column name
        for record in table:# write the rows
            writer.writerow(list(record.values()))
    return csv_fn# return the csv name

您可以從pip install獲取dbfpy和dbfread。

首先你應該知道你有什么版本的 Dbf,所以讀取文件的第一個字節:

path = "/path/to/dbf/file.dbf"
with open(path, "rb") as f:
     byte = f.read(1)
     print(f"You have a DBF {int.from_bytes(byte)} file.")

例子:

> 您有一個 DBF 3 文件。

如果你有一個 Dbf 5 文件,一切都會好起來的,但是如果,大多數情況下是我的情況,你有一個 Dbf 3 文件,你必須使用 simpledbf 調整@andy-hayden 解決方案

按照這個issue,基本上應該創建一個class的Dbf3繼承Dbf5,但是需要在_get_recs方法中添加一個新的條件。

import struct

from simpledbf import Dbf5

class Dbf3(Dbf5):
   def __init__(self, dbf, codec='utf-8'):
       super().__init__(dbf, codec)
   
   def _get_recs(self, chunk=None):
#[...copy the code from the original class up until line 664...]
               elif typ == 'M':
                   value = self._na
#[...copy the code from the original class after 664...]

原始Dbf代碼供參考

然后您的新 class Dbf3 將能夠輕松讀取和轉換 Dbf3 文件:

dbf = Dbf3(filename, codec="iso-8859-1") #codec specific to this dataset 
dbf.to_csv("converted_dbf.csv")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM