Skip to content

dbf

介绍

https://blog.csdn.net/xwebsite/article/details/6912146

python dbf 模块

1
pip install dbf

https://github.com/ethanfurman/dbf

新建数据库文件

1
2
3
4
5
6
7
8
9
import dbf

table = dbf.Table(
        filename='order_test2.dbf',
        field_specs='order_type N(5,0); price_type C(5); mode_price F(10,0); stock_code C(10); volume N(10,0); account_id C(25); act_type N(3,0); brokertype N(2,0); strategy C(20); note C(25); tradeparam C(25)',
        # on_disk=False,
)
table.open(dbf.READ_WRITE)
table.close()

新增数据

注意文件中header占一行
数据在另一行,追加的时候不会另起一行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import datetime
import dbf

table = dbf.Table(
        filename='order_test2.dbf',
        # field_specs='order_type N(5,0); price_type C(5); mode_price F(10,0); stock_code C(10); volume N(10,0); account_id C(25); act_type N(3,0); brokertype N(2,0); strategy C(20); note C(25); tradeparam C(25)',
        # on_disk=False,
)
table.open(dbf.READ_WRITE)

for datum in [
    (23, '5', 12.80, '000003', 900, '260100001983', 49, 2, dbf.Unknown, dbf.Unknown, dbf.Unknown),
]:
    table.append(datum)

table.close()

读取数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import datetime
import dbf
import time

table = dbf.Table(
        filename='/Users/nocilantro/Downloads/dbf/20210226.dbf',
        codepage="cp936",    # Chinese GBK (PRC)
        unicode_errors="ignore"
)
table.open(dbf.READ_WRITE)

print(table._meta.header.record_count)
print(type(table._table[1]))
print(table._table[1])
print(table._table[1]._meta.user_fields)

for item in table._table[1]:
    print(item)

table.close()

示例输出:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
123
<class 'dbf.Record'>
  0 - order_type: '23             '
  1 - price_type: '3                   '
  2 - mode_price: '82.97               '
  3 - stock_code: '300274    '
  4 - volume    : 1400
  5 - account_id: '010100151998             '
  6 - act_type  : 49
  7 - brokertype: 2
  8 - strategy  : '                    '
  9 - note      : '                         '
 10 - tradeparam: '                         '
['order_type', 'price_type', 'mode_price', 'stock_code', 'volume', 'account_id', 'act_type', 'brokertype', 'strategy', 'note', 'tradeparam']
23             
3                   
82.97               
300274    
1400
010100151998             
49
2

python dbfread 模块

https://github.com/olemb/dbfread

https://dbfread.readthedocs.io/en/latest/

pip install dbfread

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from dbfread import DBF

import dbf

# table = DBF('XHPT_WT20200618.dbf')
# table = DBF('test_table.dbf', ignore_missing_memofile=True)
# table = DBF('sample.dbf', ignore_missing_memofile=True)
# table = DBF('test.dbf', ignore_missing_memofile=True)
# table = DBF('wt.dbf', ignore_missing_memofile=True)
# table = DBF('XHPT_WT20200619.dbf', ignore_missing_memofile=True)
# table = DBF('daochu.dbf')
# table = DBF('weituo.dbf', encoding="gb2312")
# table = DBF('test_on_disk', encoding="gb2312")
table = DBF('XT_DBF_ORDER.dbf', encoding="gb2312")

# print(table)
print(table.header)

# print(table.dbversion)
print(table.field_names)

# for r in table.records:
#     print(r)
#     print()

# for record in table:
#     for field in record:
#         print(field, "=", record[field], end = ",")
#     print()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from dbfread import DBF

table = DBF(r"A:\orders\XT_WTCX_Stock_20210204.dbf", encoding="gb18030", char_decode_errors="ignore")

print(table.header)

print(table.field_names)

cnt = 0
for r in table.records:
    cnt += 1
    print(r)
    print()
print(cnt)