How to modify dbf files in Python

前端 未结 2 956
太阳男子
太阳男子 2021-01-06 06:27

Suppose I have different number of dbf files in some folders under the root directory, d:\\myfolder. The content of a dbf file looks like the following:

相关标签:
2条回答
  • 2021-01-06 07:22

    Using the above-mentioned dbf library (plus my other library, antipathy), these are (roughly) the steps you would take:

    # untested
    
    import dbf
    from antipathy import Path
    
    for path, dirs, files in Path.walk('.'):
        files = [f for f in files if f.ext == '.dbf']
        for db in files:
            if I_want_to_change_this_table(db):
                with dbf.Table(db) as db:
                    db.add_fields('Field2 C(4)')
                    for record in db:
                        dbf.write(Field2=record.Field1[-4:])
                    db.delete_fields('Field1')
                    db.pack()
    

    The I_want_to_change_this_table() is a function you provide if you don't want to change every table, just some of them. If you want to change them all you can remove that line.

    0 讨论(0)
  • 2021-01-06 07:28

    You would need the module called dbf available via pypi (pip install dbf). Here's a snippet of how you can add and delete fields from a table:

    import dbf
    
    table = dbf.Table('t1', 'field1 N(12, 0)')
    for record in ((11110481123,), (12150480021,)):
        table.append(record)
    table.close()
    
    # extend the existing table
    dbf.add_fields('t1', 'field2 N(4, 0)')
    
    table = dbf.Table('t1')
    records = table.sql('select *')
    for record in records:
        record.field2 = int(str(record.field1)[-4:])
    table.close()
    
    dbf.delete_fields('t1', 'field1')
    

    Though it would be less computational intensive to just go over the first field and alter it to store the last 4 digits of its value.

    0 讨论(0)
提交回复
热议问题