ship.utils.dbfread package

Submodules

ship.utils.dbfread.codepages module

guess_encoding(language_driver)[source]

ship.utils.dbfread.dbf module

Class to read DBF files.

class DBF(filename, encoding=None, ignorecase=True, lowernames=False, parserclass=<class 'ship.utils.dbfread.field_parser.FieldParser'>, recfactory=<class 'collections.OrderedDict'>, load=False, raw=False, ignore_missing_memofile=False)[source]

Bases: object

DBF table.

dbversion
deleted

Deleted records. When loaded a list of records, when not loaded a new RecordIterator object.

load()[source]

Load records into memory.

This loads both records and deleted records. The records and deleted attributes will now be lists of records.

loaded

True if records are loaded into memory.

records

Records (not included deleted ones). When loaded a list of records, when not loaded a new RecordIterator object.

unload()[source]

Unload records from memory.

The records and deleted attributes will now be instances of RecordIterator, which streams records from disk.

class RecordIterator(table, record_type)[source]

Bases: object

expand_year(year)[source]

Convert 2-digit year to 4-digit year.

ship.utils.dbfread.dbversions module

Based on the list found here:

http://www.dbf2002.com/dbf-file-format.html

get_dbversion_string(dbversion)[source]

ship.utils.dbfread.deprecated_dbf module

class DeprecatedDBF(filename, encoding=None, ignorecase=True, lowernames=False, parserclass=<class 'ship.utils.dbfread.field_parser.FieldParser'>, recfactory=<class 'collections.OrderedDict'>, load=False, raw=False, ignore_missing_memofile=False)[source]

Bases: ship.utils.dbfread.dbf.DBF, list

This is the old version of the table which is a subclass of list.

It is included for backwards compatability with 1.0 and older.

load()[source]
loaded
unload()[source]
open(filename, load=True, **kwargs)[source]
read(filename, load=True, **kwargs)[source]
warn(message)[source]

ship.utils.dbfread.exceptions module

exception DBFNotFound[source]

Bases: OSError

Raised if the DBF file was not found.

exception MissingMemoFile[source]

Bases: OSError

Raised if the corresponding memo file was not found.

ship.utils.dbfread.field_parser module

Parser for DBF fields.

class FieldParser(table, memofile=None)[source]

Bases: object

field_type_supported(field_type)[source]

Checks if the field_type is supported by the parser

field_type should be a one-character string like ‘C’ and ‘N’. Returns a boolen which is True if the field type is supported.

parse(field, data)[source]

Parse field and return value

parse0(field, data)[source]

Parse flags field and return as byte string

parse2B(field, data)

Parse integer or autoincrement field and return int.

parse40(field, data)

Parse time field (T)

Returns datetime.datetime or None

parseB(field, data)[source]

Binary memo field or double precision floating point number

dBase uses B to represent a memo index (10 bytes), while Visual FoxPro uses it to store a double precision floating point number (8 bytes).

parseC(field, data)[source]

Parse char field and return unicode string

parseD(field, data)[source]

Parse date field and return datetime.date or None

parseF(field, data)[source]

Parse float field and return float or None

parseG(field, data)[source]

OLE Object stored in memofile.

The raw data is returned as a binary string.

parseI(field, data)[source]

Parse integer or autoincrement field and return int.

parseL(field, data)[source]

Parse logical field and return True, False or None

parseM(field, data)[source]

Parse memo field (M, G, B or P)

Returns memo index (an integer), which can be used to look up the corresponding memo in the memo file.

parseN(field, data)[source]

Parse numeric field (N)

Returns int, float or None if the field is empty.

parseO(field, data)[source]

Parse long field (O) and return float.

parseP(field, data)[source]

Picture stored in memofile.

The raw data is returned as a binary string.

parseT(field, data)[source]

Parse time field (T)

Returns datetime.datetime or None

parseY(field, data)[source]

Parse currency field (Y) and return decimal.Decimal.

The field is encoded as a 8-byte little endian integer with 4 digits of precision.

class InvalidValue[source]

Bases: bytes

ship.utils.dbfread.ifiles module

Functions for dealing with mixed-case files from case-preserving file systems.

Todo

  • handle patterns that already have brackets
ipat(pat)[source]

Convert glob pattern to case insensitive form.

ifnmatch(name, pat)[source]

Case insensitive version of fnmatch.fnmatch()

iglob(pat)[source]

Case insensitive version of glob.glob()

ifind(pat, ext=None)[source]

Look for a file in a case insensitive way.

Returns filename it a matching file was found, or None if it was not.

ship.utils.dbfread.memo module

Reads data from FPT (memo) files.

FPT files are used to varying lenght text or binary data which is too large to fit in a DBF field.

VFP == Visual FoxPro DB3 == dBase III DB4 == dBase IV

class BinaryMemo[source]

Bases: ship.utils.dbfread.memo.VFPMemo

class DB3MemoFile(filename)[source]

Bases: ship.utils.dbfread.memo.MemoFile

dBase III memo file.

class DB4MemoFile(filename)[source]

Bases: ship.utils.dbfread.memo.MemoFile

dBase IV memo file

class FakeMemoFile(filename)[source]

Bases: ship.utils.dbfread.memo.MemoFile

class MemoFile(filename)[source]

Bases: object

class ObjectMemo[source]

Bases: ship.utils.dbfread.memo.BinaryMemo

class PictureMemo[source]

Bases: ship.utils.dbfread.memo.BinaryMemo

class TextMemo[source]

Bases: ship.utils.dbfread.memo.VFPMemo

class VFPMemo[source]

Bases: bytes

class VFPMemoFile(filename)[source]

Bases: ship.utils.dbfread.memo.MemoFile

find_memofile(dbf_filename)[source]
open_memofile(filename, dbversion)[source]

ship.utils.dbfread.struct_parser module

Parser that converts (C style) binary structs named tuples.

The struct can be read from a file or a byte string.

class StructParser(name, format, names)[source]

Bases: object

read(file)[source]

Read struct from a file-like object (implenting read()).

unpack(data)[source]

Unpack struct from binary string and return a named tuple.

ship.utils.dbfread.test_field_parser module

class MockDBF[source]

Bases: object

class MockField(type='', **kwargs)[source]

Bases: object

class MockHeader[source]

Bases: object

dbversion = 2
class MockMemoFile[source]

Bases: dict

make_field_parser(field_type, dbversion=2, memofile=None)[source]
test_0()[source]
test_B()[source]
test_C()[source]
test_D()[source]
test_F()[source]
test_I()[source]
test_L()[source]
test_M()[source]
test_N()[source]
test_O()[source]

Test double field.

test_T()[source]
test_Y()[source]
test_hex_field()[source]

ship.utils.dbfread.test_ifiles module

ship.utils.dbfread.test_invalid_value module

test_repr()[source]
test_type()[source]

ship.utils.dbfread.test_memo module

test_missing_memofile()[source]

ship.utils.dbfread.test_read_and_length module

Tests reading from database.

loaded_table()[source]
table()[source]
test_len()[source]
test_list()[source]

Module contents

Read DBF files with Python.

Example

>>> from dbfread import DBF
>>> for record in DBF('people.dbf'):
...     print(record)
OrderedDict([('NAME', 'Alice'), ('BIRTHDATE', datetime.date(1987, 3, 1))])
OrderedDict([('NAME', 'Bob'), ('BIRTHDATE', datetime.date(1980, 11, 12))])

Full documentation at http://dbfread.readthedocs.org/