Pythonic reader and writer for DBF/XBase files
Project description
YDbf
YDbf - reading and writing DBF/XBase files in a pythonic way. The library written in pure Python and have no external dependencies.
YDbf is compatible with Python 2.7+ and 3.5+.
What YDbf is good for:
- export data to a DBF file
- import data from a DBF file
- read data from a DBF file as a stream
Where YDbf is not a good fit:
- random access to records in a DBF file
- memo fields
Read DBF
The entrypoint of YDbf is open
function:
dbf = ydbf.open('simple.dbf')
You can use file name, or already opened in binary mode file:
fh = open('simple.dbf', 'rb')
dbf = ydbf.open(fh)
for record in dbf:
...
You may also use with
statement:
with ydbf.open('simple.dbf') as dbf:
for record in dbf:
...
Each record is a dict, which keys are names of fields.
Write DBF
YDbf opens file for reading by default, but you may set option mode
to
open for writing:
dbf = ydbf.open('simple.dbf', 'w', fields)
or open file yourself:
fh = open('simple.dbf', 'wb')
dbf = ydbf.open(fh, 'w', fields)
fields
is a structure description of DBF file, it is a required option for
write mode. The structure is as sequence of field descriptions,
where each fields described by tuple (NAME, TYPE, SIZE, DECIMAL). NAME
is a name of field, TYPE -- DBF type of field ('N' for number, 'C' for char,
'D' for date, 'L' for logical), DECIMAL is a precision (useful for 'N' type only).
For example:
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 forthe DBF file. UTF-8 is not supported, you may use only 8-bit encodings.
dbf = ydbf.open('simple.dbf', 'w', fields, encoding='cp1251')
dbf.write(data)
YDbf gets data
as an iterator where each item is a dict, which
keys are names of fields. For example,
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
File details
Details for the file YDbf-0.4.tar.gz
.
File metadata
- Download URL: YDbf-0.4.tar.gz
- Upload date:
- Size: 24.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.57.0 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa1f2808219127a10cc5b984634dfe3f18a9880409d2cf51cae9075f770af983 |
|
MD5 | f7cdc4b4b35e3450de9778c3b658b738 |
|
BLAKE2b-256 | 66754098615a0d930855441745f3061bf3cf66afaa3f47a5932e8da42538c676 |