方法将dbf转换为csv在python? [英] Way to convert dbf to csv in python?

查看:3551
本文介绍了方法将dbf转换为csv在python?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹有一堆dbf文件我想转换为csv。我试过使用代码只是将扩展名从.dbf更改为.csv,这些文件打开时,我使用Excel,但是当我打开它们在熊猫,他们看起来像这样:

I have a folder with a bunch of dbf files I would like to convert to csv. I have tried using a code to just change the extension from .dbf to .csv, and these files open fine when I use Excel, but when I open them in pandas they look like this:

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

这不是我想要的,这些字符不会出现在真实文件中。

我应该如何阅读在dbf文件中正确?

This is not what I want, and those characters don't appear in the real file.
How should I read in the dbf file correctly?

推荐答案

在线查看,有几个选项:

Looking online, there's a few options:

  • https://gist.github.com/ryanhill29/f90b1c68f60d12baea81
  • http://pandaproject.net/docs/importing-dbf-files.html
  • https://geodacenter.asu.edu/blog/2012/01/17/dbf-files-and-p
  • https://pypi.python.org/pypi/simpledbf

使用 simpledbf

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







Tweaked from the gist:

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

这篇关于方法将dbf转换为csv在python?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆