Search code examples
pythonpython-3.xdbf

Using dbf.Table: how to create index with two or more fields?


Can I create one or more fields to index on in the dbf module..

empInfo_table = dbf.Table('C:\Sonichr\\empInfo.DBF')
empInfo_table.open()
empInfo_index = empInfo_table.create_index(key = lambda rec: rec.storeid, rec.ssn - I get a syntax error)
print(empInfo_index)
for empInfo_rec in empInfo_table:      
    ssn = empInfo_rec.ssn
    storeid = empInfo_rec.storeid
            

Solution

  • The problem you are having is one of Python syntax.

    lambda arg1, arg2: some_expression, # lambda ends at first comma after the colon
    

    To protect the comma and make it part of what lambda returns, you parentheses:

    lambda rec: (rec.storeid, rec.ssn)