python编辑dbf文件,python - 无法写入dbf文件

在Python3中使用dbflib处理.dbf文件时遇到问题,尝试打开文件进行读写操作时抛出错误。错误信息提示打开模式必须是'read-write'或'read-only'。解决方法是使用Tbl.open(mode=dbf.READ_WRITE)来确保能写入数据。此问题在dbf库的新版本中需要特别注意打开模式的设置。
摘要由CSDN通过智能技术生成

卡在带有dbf lib的python3中的create dbf文件中。

我尝试了-import dbf

Tbl = dbf.Table( 'sample.dbf', 'ID N(6,0); FCODE C(10)')

Tbl.open('read-write')

Tbl.append()

with Tbl.last_record as rec:

rec.ID = 5

rec.FCODE = 'GA24850000'

并有下一个错误:

Traceback (most recent call last):

File "c:\Users\operator\Desktop\2.py", line 3, in

Tbl.open('read-write')

File "C:\Users\operator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\dbf\__init__.py", line 5778, in open

raise DbfError("mode for open must be 'read-write' or 'read-only', not %r" % mode)

dbf.DbfError: mode for open must be 'read-write' or 'read-only', not 'read-write'

如果即时通讯删除“读写”-接下来:

Traceback (most recent call last):

File "c:\Users\operator\Desktop\2.py", line 4, in

Tbl.append()

File "C:\Users\operator\AppData\Local\Programs\Python\Python36-32\lib\site-packages\dbf\__init__.py", line 5492, in append

raise DbfError('%s not in read/write mode, unable to append records' % meta.filename)

dbf.DbfError: sample.dbf not in read/write mode, unable to append records

多数民众赞成我做错了吗?如果我不尝试追加,我只获取带有正确列的.dbf,因此dbf库有效。

最佳答案

我有同样的错误。

在旧版本的dbf模块中,我可以通过仅使用以下命令打开dbf文件来编写这些文件:

Tbl.open()

但是,对于新版本(dbf.0.97),我必须使用

Tbl.open(mode=dbf.READ_WRITE)

为了能够写它们。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值