【问题标题】:how to update a record with the dbf module如何使用 dbf 模块更新记录
【发布时间】:2015-12-07 15:25:29
【问题描述】:

如何使用 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()`

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

【问题讨论】:

    标签: python python-3.x dbf xbase


    【解决方案1】:

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

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

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

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

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

    • 在dbf 模块中使用write 函数:

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

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

    【讨论】:

    • 非常感谢您提供的信息,但是这种“奇怪”的方式写入 dbf 文件的原因是什么?
    【解决方案2】:

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

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

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

    这应该更安全Search in DBF and update record

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 1970-01-01
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多