【问题标题】:can't write dbf file无法写入 dbf 文件
【发布时间】:2018-03-16 06:12:55
【问题描述】:

坚持使用 dbf lib 在 python3 中创建 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


    【解决方案1】:

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

    但是,对于新版本 (dbf.0.97),我必须使用以下命令打开文件 Tbl.open(mode=dbf.READ_WRITE) 为了能够写出来。

    【讨论】:

    • 你能给我举个例子,你如何打开文件和添加行吗?我只是传递写入错误,但无法插入行,新错误 - dbf.FieldMissingError: 'ID: no such field in table'
    【解决方案2】:

    这是一个附加示例:

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多