Search code examples
pythondbfarcpy

How to divide a dbf table to two or more dbf tables by using python


I have a dbf table. I want to automatically divide this table into two or more tables by using Python. The main problem is, that this table consists of more groups of lines. Each group of lines is divided from the previous group by empty line. So i need to save each of groups to a new dbf table. I think that this problem could be solved by using some function from Arcpy package and FOR cycle and WHILE, but my brain cant solve it :D :/ My source dbf table is more complex, but i attach a simple example for better understanding. Sorry for my poor english.

Source dbf table:

ID  NAME   TEAM
1   A      1
2   B      2
3   C      1
4   
5   D      2
6   E      3

I want get dbf1:

ID  NAME   TEAM
1   A      1
2   B      2
3   C      1

I want get dbf2:

ID  NAME   TEAM
1   D      2
2   E      3

Solution

  • Using my dbf package it could look something like this (untested):

    import dbf
    
    source_dbf = '/path/to/big/dbf_file.dbf'
    base_name = '/path/to/smaller/dbf_%03d'
    
    sdbf = dbf.Table(source_dbf)
    i = 1
    ddbf = sdbf.new(base_name % i)
    
    sdbf.open()
    ddbf.open()
    for record in sdbf:
        if not record.name:    # assuming if 'name' is empty, all are empty
            ddbf.close()
            i += 1
            ddbf = sdbf.new(base_name % i)
            continue
        ddbf.append(record)
    ddbf.close()
    sdbf.close()