python to open dbf?

1695
2
03-27-2013 06:22 AM
ChrisJ
by
New Contributor III
hi guys, python novice here, thanks for any help....

have this working so far:

import arcgisscripting
import re
import dbfpy
import sqlite3

# create the geoprocessor object
gp = arcgisscripting.create(9.3)

gp.workspace = "I:\PDtest"

layer1 = raw_input('what is layer1?')
layer1 = layer1 + ".shp"
layer2 = raw_input('what is layer2?')
layer2 = layer2 + ".shp"

# process
gp.PointDistance_analysis(layer1,layer2,"temp.dbf","#")

layer1 = re.sub('.shp', '', layer1)
layer2 = re.sub('.shp', '', layer2)

final_layer = layer1+"_"+layer2
complete_name = final_layer+".dbf"
gp.Rename_management("temp.dbf", complete_name)

...it runs a point distance analysis on two layers input by the user, and renames the resulting dbf to a combination of the two layer names.  this is pretty basic stuff but I'm new to this; now I want to open that resultant .dbf and use sqlite to interrogate it; I've imported dbfpy but can't seem to get past errors indicating that something is wrong with my dbf syntax....for example...

db = dbf.Dbf(complete_name)
for rec in db:
    print rec
print

results in 'name dbf is not defined', and several edits to this result in similar problems; what am I doing wrong when trying to open this .dbf?  I seem to have dbfpy in the proper place so that shouldn't be an issue...

thanks very much,

cj
Tags (2)
0 Kudos
2 Replies
MathewCoyle
Frequent Contributor
dbf appears to be a function in the dbfpy module. To use your syntax you would have this in your imports.
from dbfpy import dbf

Or call it explicitly in your line
db = dbfpy.dbf.Dbf(complete_name)

I've never used the package dbfpy though, so I could be off.
0 Kudos
ChrisJ
by
New Contributor III
thanks for replying mc; I've just tried your suggestion and while it hasn't worked yet, I see what you mean; will keep trying and come back if it works, thanks again...

edit: haven't gotten this going yet after many combinations of suspect syntax....what I'm trying to do is:

1) make a dbf (DONE)
2) open the dbf, so that I can:
3) use sqlite to interrogate the dbf (I need to use more powerful sql than the feeble sql box in arcmap's select by attribute box - why IS it so poor just out of curiosity?)

thanks very much all...

cj
0 Kudos