admin管理员组文章数量:1516870
今天要从同事发给我的一个文件中统计一些数字,一看还是数据库文件,以DBF结尾,近1个G呢。电脑上也没装ACCESS等数据库管理软件。后来找了个DBF阅读器,发现虽然能打开,但是筛选什么的不方便,也不好导出EXCEL表!怎么搞?当前是看看python能不能帮忙哈!
网上找了很多python关于dbf文件操作的项目。最后还是发现用dbfread比较方便。项目地址:
一、安装
pip install dbfread
二、打开一个DBF文件
>>> fromdbfreadimportDBF>>> table=DBF('people.dbf')
>>> forrecordintable:... print(record)OrderedDict([('NAME', 'Alice'), ('BIRTHDATE', datetime.date(1987, 3, 1))])OrderedDict([('NAME', 'Bob'), ('BIRTHDATE', datetime.date(1980, 11, 12))])
>>> len(table)2
>>> forrecordintable.deleted:... print(record)OrderedDict([('NAME', 'Deleted Guy'), ('BIRTHDATE', datetime.date(1979, 12, 22))])>>> len(table.deleted)
二、一盘情况下,一次只缓存一条数据在内存中。其它的都是直接从硬盘中读取。但是可以通过参数
load=True
,来进行全部加载。这种加载是随机的:
table.load()
.
This is useful when you want to look at the header before you commit to loading anything. For example, you can make a function which returns a list of tables in a directory and load only the ones you need.
If you just want a list of records and you don’t care about the other table attributes you can do:
You can unload records again with
table.unload()
.
If the table is not loaded, the
records
and
deleted
attributes
return
RecordIterator
objects.
Loading or iterating over records will open the DBF and memo file once for each iteration. This means the
DBF
object
doesn’t hold any files open, only the
RecordIterator
object
does.
版权声明:本文标题:从入门到精通:利用dbfread库操作DBF数据 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1770742038a3258026.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论