python - 根据接口文档创建DBF

前一个文章里(DBF文件格式及读写实践https://blog.csdn.net/qq_37016994/article/details/116780062?spm=1001.2014.3001.5501)仅是对现有的dbf文件进行读写操作,现补充创建dbf文件的代码,如下:
其中config.txt里定义了dbf文件的接口,接口文件:http://www.chinaclear.cn/zdjs/editor_file/20200814174418115.pdf

#字段名,字段描述,类型,长度
SXJGBS,机构标识,C,10
SXJYDM,交易代码,C,4
SXSQLS,申请交易流水,N,8
SXMXZH,资金账户,C,20
SXDWMC,单位名称,C,200
SXFSJE,发生金额,N,16,2
SXHBDH,币种,C,3
SXSKHH,收款银行行号,C,15
SXSKZH,收款银行账号,C,30
SXFKHH,付款银行行号,C,15
SXFKZH,付款银行账号,C,30
SXFKHM,付款银行名称,C,40
SXFKTD,付款银行提单号,C,8
SXJZRQ,银行进账日期,D,8
SXCXLS,撤销交易流水,N,8
SXBMMY,变码密押,C,8
SXZYNR,摘要内容,C,60
SXSQRQ,申请日期,D,8
SXSQSJ,申请时间,C,8
SXDQYE,当前余额,N,16,2
SXYEJS,余额积数,N,18,2
SXSKBS,收款笔数,N,4,0
SXSKJE,收款金额,N,16,2
SXCSBS,撤销收款笔数,N,4,0
SXCSJE,撤销收款金额,N,16,2
SXFKBS,付款笔数,N,4,0
SXFKJE,付款金额,N,16,2
SXCFBS,撤销付款笔数,N,4,0
SXCFJE,撤销付款金额,N,16,2
SXXYLS,响应交易流水,N,8
SXXYRQ,响应日期,D,8
SXXYSJ,响应时间,C,8
SXXYJG,响应结果,C,3
SXCLBZ,处理标志,C,1
SXBYZD,备用字段,C,6
SXYWLB,业务类别,C,4
SXBYZF,备用字符,C,20
SXBYSZ,备用数字,N,16,2

具体创建代码如下,其中将之前的读写代码也合并进去了,因为仅是为了实现功能,并没有对代码进行封装,有需要者自行封装啦~

#coding=utf-8
import datetime,struct

#根据接口文档和DBF文件格式要求创建ZJSXK.DBF文件
filepath="ZJSXK.DBF"
head_format_first = "BBBBIHH20x"
head_format_last = "11sc4xBB14x"
num = 0
time2 = datetime.datetime.now()
y = time2.strftime("%y")
m = time2.strftime("%m")
d = time2.strftime("%d")
ver = 3
record_number = 0
length = 0
record_list = []
with open("config.txt","r",encoding='UTF-8') as f:
    line = f.readline().strip('\ufeff')
    while line !=  '':
        if line[0] != "#":
            length = length + int(line.split(',')[3])
            num = num + 1
            recordstr = line.split(',')
            if len(recordstr) > 4:
                record_list.append([recordstr[0],recordstr[2],int(recordstr[3]),int(recordstr[4])])
            else:
                record_list.append([recordstr[0], recordstr[2], int(recordstr[3]), 0])
        line = f.readline()
len_linebyte = length + 1
len_headbyte = 33 + num*32

with open(filepath,"wb+") as f:
    hdr = struct.pack(head_format_first,ver,int(y),int(m),int(d),record_number,len_headbyte,len_linebyte)
    f.write(hdr)
    for record in record_list:
        filed = struct.pack(head_format_last,bytes(record[0],encoding="utf-8"),bytes(record[1],encoding="utf-8"),record[2],record[3])
        f.write(filed)
    f.write(b"\r")

#之前博客的代码:https://blog.csdn.net/qq_37016994/article/details/116780062?spm=1001.2014.3001.5501
# 尾部写入10条新的数据
with open(filepath, "rb+") as f:
    var, year, month, day, num, headbyte, recodebyte = struct.unpack("<4BIHH", f.read(12))
    print(var, year, month, day, num, headbyte, recodebyte)

    end_index = headbyte + 1 + recodebyte * num  # 定位到最后的位置的下一行记录的首地址
    f.seek(end_index)
    r2, r3, r4, r5, r6, r7, r8 = "4020".encode("utf-8"), "B10100001".encode("utf-8"), "shenzhen".encode(
        "utf-8"), "shenzhen".encode("utf-8"), "shanghai".encode("utf-8"), "shanghai".encode("utf-8"), "we".encode(
        "utf-8")
    for i in range(10):  # 写入十条记录
        f.write(
            struct.pack("<10s4s8x20s219x15s30s15s30s40s296xc", ("JGDMGH" + str(i)).encode("utf-8"), r2, r3, r4, r5, r6,
                        r7, r8, " ".encode("utf-8")))  # 其中格式符都是使用s

    f.seek(0)
    now = datetime.datetime.now()
    year, month, day = now.year - 1900, now.month, now.day  # 必须对应更新头记录的信息
    f.write(struct.pack("<4BIHH", var, year, month, day, num + 10, headbyte, recodebyte))

# 读取第recode个记录
with open(filepath, "rb") as f:
    var, year, month, day, num, headbyte, recodebyte = struct.unpack("<4BIHH", f.read(12))
    print(var, year, month, day, num, headbyte, recodebyte)

    recode = 5  # 第5个记录
    first_index = headbyte + recodebyte * (recode - 1) + 1  # 第五个记录的首地址

    f.seek(first_index)
    r1, r2, r3, r4, r5, r6, r7, r8, r9 = struct.unpack("<10s4s8x20s219x15s30s15s30s40s296xc", f.read(recodebyte))
    print(r1.decode("utf-8").strip(), r2.decode("utf-8").strip(), r3.decode("utf-8").strip(),
          r4.decode("utf-8").strip(), r5.decode("utf-8").strip(), r6.decode("utf-8").strip(),
          r7.decode("utf-8").strip(), r8.decode("utf-8").strip(), r9.decode("utf-8"))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

merlin’s girl

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值