Skip to content Skip to sidebar Skip to footer

Modify A Dbf File

I want to modify one column in a .dpf file using Python with this library http://pythonhosted.org/dbf/. When I want to print out some column, it works just fine. But when I am tryi

Solution 1:

My apologies for the state of the docs. Here are a couple options that should work:

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

or

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

Solution 2:

I have been trying to make a change to my dbf file for several days and searched and browsed several websites, this page was the only one that gave me a solution that worked. Just to add a little more information so that whoever lands here would understand the above piece of code that Ethan Furman shared.

import dbf
table = dbf.Table('your_dbf_filename.dbf')
# Process will openandclose a tableifnot already opened
for record in dbf.Process(table):
    record.your_column_name = 'New_Value_of_that_column'

Now, because you don't have a condition mentioned here, you would end you updating all the rows of your column. Remember, this statement will immediately reflect the new value in that column. So, the advice is to save a copy of this dbf file before making any edits to it. I also tried the 2nd solution that Ethan mentions, but it throws an error that 'table' not defined.

Post a Comment for "Modify A Dbf File"