简体   繁体   English

如何使用python dbf模块从dbf文件中删除记录?

[英]How to delete record from dbf file using python dbf module?

I'm trying to write/delete records in a visual foxpro 6 dbf file, using python 2.7 and the dbf package:我正在尝试使用 python 2.7 和 dbf 包在可视 Foxpro 6 dbf 文件中写入/删除记录:

import dbf
tbl = dbf.Table('test.dbf')
tbl.open()
rec = tbl[0]
print(rec.__class__)
rec.delete_record()

Result:结果:

<class 'dbf.ver_2.Record'>
Traceback (most recent call last):
  File "C:/Python/Projects/test/test.py", line 11, in <module>
    rec.delete_record()
  File "C:\Python\Projects\test\venv\lib\site-packages\dbf\ver_2.py", line 2503, in __getattr__
    raise FieldMissingError(name)
dbf.ver_2.FieldMissingError: 'delete_record:  no such field in table'

Here is the documentation for that package: http://pythonhosted.org/dbf/这是该包的文档: http : //pythonhosted.org/dbf/

The record object really does not have this method, but it is documented.记录对象确实没有这个方法,但它被记录在案。 The table is opened in read-write mode.该表以读写模式打开。 (But it is also true that the Table() constructor should return an opened table, but it returns a closed table instead.) (但 Table() 构造函数也应该返回一个打开的表,但它返回的是一个关闭的表。)

What am I doing wrong?我究竟做错了什么?

The biggest problem is that there are no other options.最大的问题是没有其他选择。 The only other package I know of is "dbfpy" but that does not handle vfoxpro 6 tables, and it does not handle different character encodings.我知道的唯一其他包是“dbfpy”,但它不处理 vfoxpro 6 表,也不处理不同的字符编码。

That documentation is out of date. 该文档已过时。 (My apologies.) (我很抱歉。)

What you want is: 您想要的是:

dbf.delete(rec)
tbl.pack()

Alternatively you can use dbfpy module which allows you to access records public variable deleted .或者,您可以使用dbfpy模块,该模块允许您访问记录公共变量删除 Example code:示例代码:

from dbfpy import dbf
...
dbfFile = dbf.Dbf("DbfFileName", readOnly=True)
for rec in dbfFile:
  if rec.deleted:
    #action if deleted
dbfFile.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM