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, TYP – DBF 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file ydbf-py3-0.7.post20180112.tar.gz
.
File metadata
- Download URL: ydbf-py3-0.7.post20180112.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1d81b819524ca0191c47057c728a2b6ef76141370574af29e0b7a299211f30ab |
|
MD5 | bd6d579e3b46c23f86a0a9a702c01ce9 |
|
BLAKE2b-256 | 34d80f3c98f6b189049e884d6d5cbd6a284ec52410384fde0380870a8d23afc6 |
File details
Details for the file ydbf_py3-0.7.post20180112-py2.py3-none-any.whl
.
File metadata
- Download URL: ydbf_py3-0.7.post20180112-py2.py3-none-any.whl
- Upload date:
- Size: 23.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e950872706b58c1e5e560b0b7fbea6769ebdf0fbc675dea72b58726575d96d6a |
|
MD5 | 38ec147ea43c786095ba6f5566923fc0 |
|
BLAKE2b-256 | 2f5027d640c356bf04bf9120a600dde2788b8322bd9d46a5cd1d88851571345a |