I have a couple thousand .DBF files which need to be updated. These files currently have a header with one or two columns (Name, Geopath) or (Name), and a single row of data.
Updates include:
- deleting the geopath column if it exists
- adding additional columns
- populating the new columns in the (only) data row with data from a tab delimited file.
I am unable to add the columns and populate them. (See below for error)
At a high level, my intention is to:
Loop through the records in a source file For each record: Use the 'FileName' in the record to open the appropriate .dbf file (in the current directory) Add additional column headers Populate the new columns with data from the current record in the source file
Source file - tab delimited, with columns: GUID C(24) VenueName C(100) Network C(50) Address1 C(75) City C(50) SVID Float Filename (ex: test1.dbf)
This works for step 1:
import dbf
file = open('testing.tsv')
for line in file: #'GUID\tVenueName\tNetworkName\tAddress1\tCity\tSalesVenueID\n'
fields = line.strip().split('\t')
res=[fields[0], fields[1], fields[2], fields[3], fields[4],fields[5], fields[6]]
print("the elements of the list are : " + str(res))
#fields[0]=Guid, fields[1]=VenueName, fields[2]=NetworkName, fields[3] = Address1, fields[4]=City, fields[5]=SalesVenueID, fields[6]=FilePath
filename=fields[6]
print("filename is: " + str(filename))
if not str(filename)=='FileName':
with dbf.Table(str(filename)) as db:
try:
db.delete_fields('geojson')
except Exception:
pass
db.pack()
For steps 2 and 3, this slightly different snippet writes the columns but errors with 'data to append must be a tuple, dict, record, or template; not a <class 'str'>' at the table.append step.
import dbf
file = open('testing.tsv')
for line in file: #'GUID\tVenueName\tNetworkName\tAddress1\tCity\tSalesVenueID\n'
fields = line.strip().split('\t')
res=[fields[0], fields[1], fields[2], fields[3], fields[4],fields[5], fields[6]]
print("the elements of the list are : " + str(res))
#fields[0]=Guid, fields[1]=VenueName, fields[2]=NetworkName, fields[3] = Address1, fields[4]=City, fields[5]=SalesVenueID, fields[6]=FilePath
currentfilename=str(fields[6])
print("filename is: " + currentfilename)
if not currentfilename=='FileName': #Ignore header row
# create an in-memory table
table = dbf.Table(
filename=currentfilename,
field_specs='GUID C(24); VenueName C(100); Network C(50) ; Address1 C(75); City C(50); SVID C(20)',
on_disk=True,
)
table.open(dbf.READ_WRITE)
# add some records to it
for datum in (
(tuple(res))
):
table.append(datum)
# iterate over the table, and print the records
for record in table:
print(record)
print('--------')
table.pack()
See screenshot for what is in variable variable 'res'
Edited to include the final (working) code:
import dbf
file = open('SourceFile.tsv')
for line in file:
#'GUID\tVenueName\tNetworkName\tAddress1\tCity\tSalesVenueID\n'
fields = line.strip().split('\t')
res=[fields[0], fields[1], fields[2], fields[3], fields[4]]
print("the elements of the list are : " + str(res))
#fields[0]=VenueName, fields[1]=NetworkName, fields[2] = Address1,
fields[3]=City, fields[4]=SalesVenueID, fields[5]=FilePath
currentfilename=str(fields[5])
print("filename is: " + currentfilename)
if not currentfilename=='FileName': #Ignore header row
# create or open existing file
table = dbf.Table(
filename=currentfilename,
field_specs='VenueName C(100); Network C(50) ; Address1
C(75); City C(50); SVID C(20)',
on_disk=True,
)
table.open(dbf.READ_WRITE)
# Update record
VenueName=fields[1]
try:
table.append(tuple(res))
except Exception as e:
print('--------')
print('--------')
print('Error on VenueName: ' + VenueName )
print(e)
pass
# iterate over the table, and print the records
for record in table:
print(record)
print('--------')
table.pack()