Skip to main content

Pythonic reader and writer for DBF/XBase files

Project description

Read/write DBF (DBase/XBase) files from Python. This is a pure-Python implementation with no external dependencies.

Quickstart

pip install ydbf-py3

Reading

The entrypoint of YDbf is an open function:

dbf = ydbf.open('simple.dbf')

You can use file name, or already opened (in binary mode) file:

with ydbf.open('simple.dbf') as dbf:
    for record in dbf:
        ...

Each record is represented as dict, where keys are names of fields.

Writing

By default, YDbf opens file for reading, but you may set option mode to open for writing:

with ydbf.open('simple.dbf', 'w', fields) as dbf:
    dbf.write(data)

Or pass in a file handle:

with open('simple.dbf', 'wb') as fh:
    dbf = ydbf.open(fh, 'w', fields)

fields is a structure description of DBF file, it is required option for writing mode. Structure defined as sequence of field descriptions, where each fields described by tuple (NAME, TYP, SIZE, DEC). Where NAME is a name of field, TYPDBF type of field ('N' for number, 'C' for char, 'D' for date, 'L' for logical), DEC is a precision (useful for 'N' type only). For example, like this:

fields = [
    ('ID',      'N',  4, 0),
    ('VALUE',   'C', 40, 0),
    ('UPDATE',  'D', 8, 0),
    ('VISIBLE', 'L', 1, 0),
]

YDbf uses unicode for 'C' fields by default, so you may want to define encoding which be used for DBF file. Sadly, UTF-8 is not available, only old scrappy 8-bits.

dbf = ydbf.open('simple.dbf', 'w', fields, encoding='cp1251')
dbf.write(data)

YDbf gets data as iterator where each item is a dict, where keys are name of fields. For early defined example it may looks like

data = [
    {'ID': 1, 'VALUE': 'ydbf', 'VISIBLE': True,
        'UPDATE': datetime.date(2009, 7, 14)},
    {'ID': 2, 'VALUE': 'ydbf-dev', 'VISIBLE': False,
        'UPDATE': datetime.date(2009, 5, 15)},
    {'ID': 3, 'VALUE': 'pytils', 'VISIBLE': True,
        'UPDATE': datetime.date(2009, 5, 11)},
]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ydbf-py3-0.7.post20180112.tar.gz (16.2 kB view hashes)

Uploaded Source

Built Distribution

ydbf_py3-0.7.post20180112-py2.py3-none-any.whl (23.2 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page