Reputation: 15
I would like to use the DBF module to split a vfp dbf based on a value in a column, dist. I therefore extracted all necessary indices to use them for the export. Unfortunately, the original table does not allow a list of indices.
input_file = 'refrence.dbf'
table = dbf.Table(input_file)
l = []
for ind, record in enumerate(table.open(mode=dbf.READ_WRITE)):
with record as r:
if r.dist >= start and r.dist <= end:
l.append(ind)
is there an easier way to get to n separate dbf files containing only the rows which satisfy the condition r.dist >= start and r.dist <= end
?
Upvotes: 1
Views: 1574
Reputation: 546
You can access filtered table like this
table[l]["dist"]
or
table[l][fieldName]
or
table[l][0]
You should make your dbf file with this filtered data.
Upvotes: 1
Reputation: 77399
I would try something like the following (untested):
def copy_range(source, destination, field_name, start, end):
src = dbf.Table(source).open()
dst = src.new(destination).open(dbf.READ_WRITE) # copy structure
for record in src:
if start <= record[field_name] <= end:
dst.append(record)
src.close()
dst.close()
Then call it like:
copy_range('refrence.dbf', 'result.dbf', 'dist', 100, 200)
Upvotes: 1
Reputation: 69298
Selecting the records can be done by:
target_records = [rec for rec in table if start <= rec.dist <= end]
Once you have the records, it's fairly easy to copy them into a new dbf:
new_table = old_table.new('some_file.dbf')
with new_table:
for record in target_records:
new_table.append(record)
Upvotes: 1