Problem creating a table and appending

1,136 views
Skip to first unread message

Robin

unread,
Oct 15, 2012, 1:03:21 PM10/15/12
to python...@googlegroups.com
I am converting some old PC Arc/Info SMLs and FoxPro routines to python and am trying to get started with the dbf module.

With the test Script below, I receive the error message:
"TypeError: __new__() got an unexpected keyword argument 'read_only'"

when I remove the "read_only=False" I of course get an access error when I try to append a record.
"Traceback (most recent call last):
File "D:\BD\BD9649_GradientAccess\103J077\test4.py", line 6, in <module>
rec = Tbl.append()
File "C:\Python27\ArcGIS10.1\lib\site-packages\dbf.py", line 3752, in append
raise DbfError('%s not in read/write mode, unable to append records' % meta.filename)
DbfError: sample.dbf not in read/write mode, unable to append records"

TEST SCRIPT

import arcpy
import dbf

Tbl = dbf.Table( 'sample.dbf', 'ID N(6,0); FCODE C(10)', read_only=False )

rec = Tbl.append()

rec.ID = 5
rec.FCODE = 'GA24850000'
rec.write.record()

Ethan Furman

unread,
Oct 15, 2012, 11:20:06 PM10/15/12
to python...@googlegroups.com
Robin wrote:
> Tbl = dbf.Table( 'sample.dbf', 'ID N(6,0); FCODE C(10)', read_only=False )
> rec = Tbl.append()
>
> rec.ID = 5
> rec.FCODE = 'GA24850000'
> rec.write.record()

There have been a few changes in the latest version. Your code should
now look like:


Tbl = dbf.Table( 'sample.dbf', 'ID N(6,0); FCODE C(10)')
Tbl.open()
Tbl.append()
with Tbl.last_record as rec:
rec.ID = 5
rec.FCODE = 'GA24850000'

Note the .open() call, not assigning the result from .append() (it now
just returns None like list does), and using a `with` statement to
access the last record.

If you can't use the `with` statement, then try:

Tbl = dbf.Table( 'sample.dbf', 'ID N(6,0); FCODE C(10)')
Tbl.open()
rec = dbf.create_template(Tbl)
rec.ID = 5
rec.FCODE = 'GA24850000'
Tbl.append(rec)

Hope this helps!

~Ethan~
Reply all
Reply to author
Forward
0 new messages