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)},
]
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.1.tar.gz.
File metadata
- Download URL: YDbf-0.4.1.tar.gz
- Upload date:
- Size: 24.7 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 |
ea6098bcef11b7641a4c61454761728ab0abc54e918da90e18d5b5d89261f803
|
|
| MD5 |
a648b669cb06369172b37e04f0b936e6
|
|
| BLAKE2b-256 |
06107b2ceb70bde10b4620f87715bbdd7a531593ea86868a0136db6570558955
|