Python DBF with .dbf table: How to associate a . CDX index

Python DBF with .dbf table: How to associate a . CDX index … here is a solution to the problem.

Python DBF with .dbf table: How to associate a . CDX index

I was given the ambiguous task of automatically extracting data from various Visual FoxPro tables.

There are several pairs. DBF and . CDX file. Using Python dbf packages, I seem to be able to work with them. I have two files, ABC. DBF and ABC. CDX。 I can use the load table file

>>> import dbf
>>> table = dbf. Table('ABC. DBF')
>>> print(table[3])
  0 - table_key : '\x00\x00\x04'
  1 - field_1   : -1
  2 - field_2   : 0
  3 - field_3   : 34
  4 - field_ 4  : 2
  ...

>>>

As I understand it, .cdx files are indexes. I suspect corresponds to the table_key field. According to the author, dbf can read index:

I can read IDX files, but not update them. My day job changed and dbf
files are not a large part of the new one. – Ethan Furman May 26 ’16
at 21:05

All I need to do is read. I see that there are four classes, Idx, Index, IndexFile, and IndexLocation. These seem to be good candidates.

The Idx class reads in a table and file name, which is promising.

>>> index = dbf. Idx(table, 'ABC. CDX')

I’m not sure how to use this object, though. I see it has some generators, backward and forward, but I get an error when I try to use them

>>> print(list(index.forward()))
.dbf. NotFoundError: 'Record 67305477 is not in table ABC. DBF'

How do I relate a .cdx index file to a .dbf table?

Solution

Unlike .idx and .cdx, dbf cannot currently read .cdx files.

If you need to sort the table, you can create an in-memory index:

my_index = table.create_index(key=lambda r: r.table_key)

You can also create a complete function:

def active(rec):
    # do not show deleted records
    if is_deleted(rec):
        return DoNotIndex
    return rec.table_key

my_index = table.create_index(active)

Then iterate over the index instead of the table:

for record in my_index:
    ...

Related Problems and Solutions