python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python写入dbf文件内容及创建dbf文件

python如何写入dbf文件内容及创建dbf文件

作者:lover-517

这篇文章主要介绍了python如何写入dbf文件内容及创建dbf文件,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

想要实现打开dbf文件后写入数据,奈何python3网上没有任何方法,有一个人自己写了个类,基于个人要求,需要操作dbf文件实现内容添加,以下方法需要python2的环境来是实现。

需求:向八个dbf文件中插入数据

一、需要导入的模块

import string
import time
import random
import sys
import os
import csv
import getopt
from dbfpy import dbf
import itertools
import warnings
warnings.filterwarnings('ignore')

二、实现dbf文件操作

class Do_to_Dbf(object):
    def __init__(self,Cilent_id,Fund_acc,SecuAcc):
        self.Cilent_id=Cilent_id
        self.Fund_acc = Fund_acc
        self.SecuAcc=SecuAcc
# 创建新的dbf文件
    def writeDbfFile(self,filename,header,content):
        db=dbf.Dbf(filename,new=True)
        for field in header:
            if type(field) == unicode:
                field = field.encode('GBK')
            db.addField((field, 'C', 60))
        for record in content:
            rec = db.newRecord()
            for key, value in itertools.izip(header, record):
                if type(value) == unicode:
                    rec[key] = value.encode('GBK')
                else:
                    rec[key] = value
                rec.store()
        db.close()
# 再原有的dbf文件中追加内容
    def add_DbfFile_content(self,filename,content):
        db=dbf.Dbf(filename)
        for record in content:
            rec = db.newRecord()
            for key, value in itertools.izip(db.header, record):
                if type(value) == unicode:
                    rec[key] = value.encode('GBK')
                else:
                    rec[key] = value
                rec.store()
        db.close()
    def get_code(self):
        code = [[], []]
        code[0] = ['000002', '000060', '159901', '159930', '300030']
        code[1] = ['510010', '510300', '600036', '688001', '700360']
        return code
## 下边的函数create开头的都是创建新的dbf文件,根据需要的表头来 ##
## 下边的函数add开头的都是再原有dbf文件下添加内容 ##
    def create_custinfo(self):
        header=['CilentID','CilentName','BranchID','EncMode','TradePwd','Status']
        pwd = '34614C364D51746D79335231584559444D68337470673D3D'
        content=[]
        for i in range(len(self.Cilent_id)):
                part=[
                    self.Cilent_id[i],
                    self.Cilent_id[i],
                    self.Cilent_id[i][:4],
                    1,
                    pwd,
                    0]
                content.append(part)
        filename='doc/'+'custinfo_v01_12360000_3_%s.dbf'%(time.strftime('%Y%m%d_%H',time.localtime()))
        self.writeDbfFile(filename,header,content)
    def create_fundasset(self):
        header=['FundAcc','CurType','FundAvlBal','FundFrzBal','FundBal','FBuySale','FUncBuy','FUncSale','FFundTrsf']
        content=[]
        for i in range(len(self.Fund_acc)):
                part=[
                    self.Fund_acc[i],
                    0,
                    'F2195BC3951AAD0465B72CE593B79141',
                    'A160C3DB0CE4098FDCD07FC86865BF33',
                    'F2195BC3951AAD0465B72CE593B79141',
                    'A160C3DB0CE4098FDCD07FC86865BF33',
                    'A160C3DB0CE4098FDCD07FC86865BF33',
                    'A160C3DB0CE4098FDCD07FC86865BF33',
                    'BAFBDC944A98ADCD131416B325BE8D3C'
                ]
                content.append(part)
        filename='doc/'+'fundasset_v01_12360000_3_%s.dbf'%(time.strftime('%Y%m%d_%H',time.localtime()))
        self.writeDbfFile(filename,header,content)
    def create_fundinfo(self):
        header=['FundAcc','FAccType','ClientID','TradeWays','FAccStatus','FundLimits','TrdRights','FundRights','CreditFlag','CreStatus','InvestType']
        content=[]
        for i in range(len(self.Fund_acc)):
                part=[
                    self.Fund_acc[i],
                    1,
                    self.Cilent_id[i],
                    'jk012345678',
                    0,
                    '?',
                    '01234567',
                    '@',
                    0,
                    0,
                    0
                ]
                content.append(part)
        filename='doc/'+'fundinfo_v01_12360000_3_%s.dbf'%(time.strftime('%Y%m%d_%H',time.localtime()))
        self.writeDbfFile(filename,header,content)
    def create_fundtrdctrl(self):
        header=['FundAcc','Market','BSFlag','StkType','StkCode','Direct']
        content=[]
        # for i in range(len(self.Fund_acc)):
        #         part=[
        #             self.Fund_acc[i],
        #             1,
        #             2,
        #             0,
        #             '002612',
        #             1
        #         ]
        #         content.append(part)
        # print(content)
        filename='doc/'+'fundtrdctrl_v01_12360000_3_%s.dbf'%(time.strftime('%Y%m%d_%H',time.localtime()))
        self.writeDbfFile(filename,header,content)
    def create_secuinfo(self):
        header=['ClientID','SecuAcc','SAccType','Market','RegFlag','Seat','Status','SAccLimits','SecuRights']
        content=[]
        for i in range(len(self.Cilent_id)):
            for j in range(2):
                seat=['016701','']
                part=[
                    self.Cilent_id[i],
                    self.SecuAcc[j][i],
                    1,
                    j,
                    j,
                    seat[j],
                    0,
                    '',
                    '@'
                ]
                content.append(part)
        filename='doc/'+'secuinfo_v01_12360000_3_%s.dbf'%(time.strftime('%Y%m%d_%H',time.localtime()))
        self.writeDbfFile(filename,header,content)
    def create_SJSKS(self):
        header=['KSXWDM','KSGDDM','KSYWLB','KSKSGE','KSFSRQ','KSBYZD']
        content=[]
        # for i in range(len(Fund_acc)):
        #         part=[
        #             '',
        #             '',
        #             '',
        #             '',
        #             '',
        #         ]
        #         content.append(part)
        filename='doc/'+'SJSKS_v01_12360000_3_%s.dbf'%(time.strftime('%Y%m%d_%H',time.localtime()))
        self.writeDbfFile(filename,header,content)
    def create_stkasset(self):
        header=['FundAcc','Market','SecuAcc','StockCode','StkAvlQty','StkFrzQty','StkBal',
                'SBuySale','SBuyUnFrz','SSaleFrz','LstBuyCst','LstprfCst','BuyCst',
                'ProfitCst','StkRemain','StkCorpRem','CreStkBal']
        content=[]
        for i in range(len(self.Fund_acc)):
            for j in range(2):
                codes=self.get_code()
                for code in codes[j]:
                    part=[
                        self.Fund_acc[i],
                        j,
                        self.SecuAcc[j][i],
                        code,
                        'F461FA1CB2A69A4EBB12F130455748DB',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'F461FA1CB2A69A4EBB12F130455748DB',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        '77E3F6BDC6D282F4FA3841AD363517CD',
                        '77E3F6BDC6D282F4FA3841AD363517CD',
                        '77E3F6BDC6D282F4FA3841AD363517CD',
                        '77E3F6BDC6D282F4FA3841AD363517CD',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        ]
                    content.append(part)
        filename='doc/'+'stkasset_v01_12360000_3_%s.dbf'%(time.strftime('%Y%m%d_%H',time.localtime()))
        self.writeDbfFile(filename,header,content)
    def create_zqyXXXXX(self):
        header=['gddm','zqye','qysl','kcqy','gsdm','zhxh','djrq','clbz']
        content=[]
        # for i in range(len(Fund_acc)):
        #         part=[
        #             '',
        #             '',
        #             '',
        #             '',
        #             '',
        #             '',
        #             '',
        #             '',
        #         ]
        #         content.append(part)
        filename='doc/'+'zqyXXXXX_v01_12360000_3_%s.dbf'%(time.strftime('%Y%m%d_%H',time.localtime()))
        self.writeDbfFile(filename,header,content)
    def add_custinfo(self):
        content=[]
        pwd = '34614C364D51746D79335231584559444D68337470673D3D'
        for i in range(len(self.Cilent_id)):
                part=[
                    self.Cilent_id[i],
                    self.Cilent_id[i],
                    self.Cilent_id[i][:4],
                    1,
                    pwd,
                    0]
                content.append(part)
        filename='doc/custinfo_v01_12360000_3_20191014_01.dbf'
        self.add_DbfFile_content(filename,content)
    def add_fundasset(self):
        content=[]
        for i in range(len(self.Fund_acc)):
                part=[
                    self.Fund_acc[i],
                    0,
                    'F2195BC3951AAD0465B72CE593B79141',
                    'A160C3DB0CE4098FDCD07FC86865BF33',
                    'F2195BC3951AAD0465B72CE593B79141',
                    'A160C3DB0CE4098FDCD07FC86865BF33',
                    'A160C3DB0CE4098FDCD07FC86865BF33',
                    'A160C3DB0CE4098FDCD07FC86865BF33',
                    'BAFBDC944A98ADCD131416B325BE8D3C'
                ]
                content.append(part)
        filename='doc/fundasset_v01_12360000_3_20191014_01.dbf'
        self.add_DbfFile_content(filename,content)
    def add_fundinfo(self):
        content=[]
        for i in range(len(self.Fund_acc)):
                part=[
                    self.Fund_acc[i],
                    1,
                    self.Cilent_id[i],
                    'jk012345678',
                    0,
                    '?',
                    '01234567',
                    '@',
                    0,
                    0,
                    0
                ]
                content.append(part)
        filename='doc/fundinfo_v01_12360000_3_20191014_01.dbf'
        self.add_DbfFile_content(filename,content)
    def add_fundtrdctrl(self):
        content=[]
        filename='doc/fundtrdctrl_v01_12360000_3_20191014_01.dbf'
        self.add_DbfFile_content(filename,content)
    def add_secuinfo(self):
        content=[]
        for i in range(len(self.Cilent_id)):
            for j in range(2):
                seat=['016701','']
                part=[
                    self.Cilent_id[i],
                    self.SecuAcc[j][i],
                    1,
                    j,
                    j,
                    seat[j],
                    0,
                    '',
                    '@'
                ]
                content.append(part)
        filename='doc/secuinfo_v01_12360000_3_20191014_01.dbf'
        self.add_DbfFile_content(filename,content)
    def add_SJSKS(self):
        content=[]
        filename='doc/SJSKS_v01_12360000_3_20191014_01.dbf'
        self.add_DbfFile_content(filename,content)
    def add_stkasset(self):
        content=[]
        for i in range(len(self.Fund_acc)):
            for j in range(2):
                codes=self.get_code()
                for code in codes[j]:
                    part=[
                        self.Fund_acc[i],
                        j,
                        self.SecuAcc[j][i],
                        code,
                        'F461FA1CB2A69A4EBB12F130455748DB',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'F461FA1CB2A69A4EBB12F130455748DB',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        '77E3F6BDC6D282F4FA3841AD363517CD',
                        '77E3F6BDC6D282F4FA3841AD363517CD',
                        '77E3F6BDC6D282F4FA3841AD363517CD',
                        '77E3F6BDC6D282F4FA3841AD363517CD',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        'BAFBDC944A98ADCD131416B325BE8D3C',
                        ]
                    content.append(part)
        filename='doc/stkasset_v01_12360000_3_20191014_01.dbf'
        self.add_DbfFile_content(filename,content)
    def add_zqyXXXXX(self):
        content=[]
        filename='doc/zqyXXXXX_v01_12360000_3_20191014_01.dbf'
        self.add_DbfFile_content(filename,content)

三、生成需要的数据和提供脚本参数

# 因为项目要实现自动化脚本,传入两个参数,从那个账号开始,以及多少个数据 #
def message(argv):
    start_account = ""
    num = ""
    try:
        opts, args = getopt.getopt(argv, "hs:n:", ["help", "start_account=", "num="])
    except getopt.GetoptError:
        print('Error:create_dbf_file.py -s <start_account> -n <num>')
        print('   or: create_dbf_file.py --start_account=<start_account> --num=<num>')
        sys.exit(2)
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            print(' create_dbf_file.py -s <start_account> -n <num>')
            print(' create_dbf_file.py --start_account=<start_account> --num=<num>')
            sys.exit()
        elif opt in ("-s", "--start_account"):
            start_account = arg
        elif opt in ("-n", "--num"):
            num = arg
def get_account(start_account,num):
    accounts = []
    for i in range(num):
        account=start_account +i
        accounts.append(str(account))
    return accounts
def get_secuacc(tools,num):
    secuacc=[[],[]]
    for i in ['0','A']:
        if i=='0':
            passwds = secuacc[0]
        else:
            passwds = secuacc[1]
        while len(passwds)< int(num):
            part=random.sample(tools,9)
            passwd="".join(part)
            if passwd in passwds:
                continue
            else:
                passwds.append(i+passwd)
    return secuacc
# 保存数据
def create_to_csv(file_path,filename,data):
    f=open(file_path+'/'+filename,'wb')
    writer=csv.writer(f)
    a=[]
    for i in data:
            a.append(i.decode("utf-8").encode("gbk"))
    writer.writerows(a)
    f.close()
def read_to_csv(filename):
    reader=csv.reader(open(filename))
    a=[]
    for i in reader:
        s=''.join(i)
        a.append(s)
    return a

四、调用

if __name__ == '__main__':
	# 代码需要命令行执行,需要传入参数,有提示在message函数里 #
    tools=string.digits
    message(argv=sys.argv[1:])
    Cilent_id=get_account(int(sys.argv[1]),int(sys.argv[2]))
    Fund_acc=get_account(int(time.strftime('%m%d%H%S0000',time.localtime())),int(sys.argv[2]))
    SecuAcc=get_secuacc(tools,int(sys.argv[2]))
    file_path=os.getcwd()+'/data'
    if os.path.isdir(file_path):
        pass
    else:
        os.mkdir(file_path)
    create_to_csv(file_path,'Cilent_id',Cilent_id)
    create_to_csv(file_path,'Fund_acc',Fund_acc)
    create_to_csv(file_path,'SecuAcc_SH',SecuAcc[0])
    create_to_csv(file_path, 'SecuAcc_SZ', SecuAcc[1])
    add_dbffile=Do_to_Dbf(Cilent_id,Fund_acc,SecuAcc)
    # add_dbffile.create_custinfo()
    # add_dbffile.create_fundasset()
    # add_dbffile.create_fundinfo()
    # add_dbffile.create_fundtrdctrl()
    # add_dbffile.create_SJSKS()
    # add_dbffile.create_zqyXXXXX()
    # add_dbffile.create_stkasset()
    # add_dbffile.create_secuinfo()
    add_dbffile.add_custinfo()
    add_dbffile.add_fundasset()
    add_dbffile.add_fundinfo()
    add_dbffile.add_fundtrdctrl()
    add_dbffile.add_secuinfo()
    add_dbffile.add_SJSKS()
    add_dbffile.add_stkasset()
    add_dbffile.add_zqyXXXXX()

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文