今天要从同事发给我的一个文件中统计一些数字,一看还是数据库文件,以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.


发布评论