2

如何使用 dbf 模块更新 dbf 中的记录:https ://pypi.python.org/pypi/dbf

这是我尝试过的:

table2 = dbf.Table(filename, codepage="cp1252")
table[0].name = "changed"

但我得到:

  File "<input>", line 1, in <module>
  File "/venv/lib/python3.4/site-packages/dbf/ver_33.py", line 2508, in __setattr__
    raise DbfError("unable to modify fields individually except in `with` or `Process()`")
dbf.ver_33.DbfError: unable to modify fields individually except in `with` or `Process()`

我设法读取和附加但不修改数据,我该怎么做?

4

2 回答 2

2

将数据写入记录可以通过几种不同的方式完成:

  • 使用记录作为上下文管理器:

    with table[0] as rec: rec.name = "changed"

  • 使用该dbf.Process函数循环处理多个记录:

    for rec in Process(my_table): rec.name = rec.name.title()

  • 使用模块write中的函数dbf

    dbf.write(some_record, name='My New Name')

我这样做是为了提高安全性和性能。

于 2015-12-07T16:39:07.893 回答
0

好的,所以我不明白“使用或处理”部分,但这是我设法做到的:

dbf.write(table[0],name="changed")

在那里找到,这个叉子有更多文档https://bitbucket.org/ltvolks/python-dbase

这应该更安全在 DBF 中搜索并更新记录

于 2015-12-07T16:04:00.070 回答