无法写入dbf文件

问题描述 投票:1回答:2

使用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 <module>
    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 <module>
    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库工作。

python dbf
2个回答
1
投票

我有同样的错误。在旧版本的dbf模块中,我能够通过Tbl.open()打开它们来编写dbf文件

但是,对于新版本(dbf.0.97),我必须使用Tbl.open(mode=dbf.READ_WRITE)打开文件才能编写它们。


1
投票

这是一个附加示例:

table = dbf.Table('sample.dbf', 'cod N(1,0); name C(30)')
table.open(mode=dbf.READ_WRITE)
row_tuple = (1, 'Name')
table.append(row_tuple)
© www.soinside.com 2019 - 2024. All rights reserved.