繁体   English   中英

修改 DBF 文件

[英]Modify a DBF file

我想使用 Python 和这个库http://pythonhosted.org/dbf/修改 .dpf 文件中的一列。
当我想打印一些列时,它工作得很好。 但是当我尝试修改列时,出现错误

无法单独修改字段,除了withProcess()

我的代码:

table = dbf.Table('./path/to/file.dbf')
table.open()

for record in table:
    record.izo = sys.argv[2]

table.close()

在文档中,他们建议这样做

for record in Write(table):

但我也收到一个错误:

名称 'Write' 未定义并且:

record.write_record(column=sys.argv[2])

还给我一个错误

write_record - 表中没有这样的字段

谢谢!

我为文档的状态道歉。 以下是一些应该有效的选项:

table = dbf.Table('./path/to/file.dbf')
# Process will open and close a table if not already opened
for record in dbf.Process(table):
    record.izo = sys.argv[2]

with dbf.Table('./path/to/file.dbf')
    # with opens a table, closes when done
    for record in table:
        with record:
            record.izo = sys.argv[2]

几天来我一直试图更改我的 dbf 文件并搜索和浏览了几个网站,这个页面是唯一一个给我一个有效解决方案的页面。 只是添加更多信息,以便任何登陆这里的人都能理解 Ethan Furman 共享的上述代码。

import dbf
table = dbf.Table('your_dbf_filename.dbf')
# Process will open and close a table if not already opened
for record in dbf.Process(table):
    record.your_column_name = 'New_Value_of_that_column'

现在,因为您没有此处提到的条件,所以您将结束更新列的所有行。 请记住,此语句将立即反映该列中的新值。 因此,建议是在对其进行任何编辑之前保存该 dbf 文件的副本。 我还尝试了 Ethan 提到的第二个解决方案,但它引发了一个错误,即“表”未定义。

暂无
暂无

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

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