2

How can I update a record in a dbf with the dbf module : https://pypi.python.org/pypi/dbf

Here is what I tried:

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

But I get:

  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()`

I managed to read and append but not to modify data, how can I do that?

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
maazza
  • 7,016
  • 15
  • 63
  • 96

2 Answers2

2

Writing data to records can be accomplished in a few different ways:

  • using the record as a context manager:

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

  • using the dbf.Process function to deal with several records in a loop:

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

  • using the write function in the dbf module:

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

I did it this way to enhance both safety and performance.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • thanks a lot for the info but what were the reasons for this "strange" way to write to a dbf file ? – maazza Dec 07 '15 at 19:28
0

OK, soo I didn't understand the "with or process" part but here is how I managed to get my way:

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

found there, this fork had more documentation https://bitbucket.org/ltvolks/python-dbase

this should be safer Search in DBF and update record

Community
  • 1
  • 1
maazza
  • 7,016
  • 15
  • 63
  • 96