dbf icon indicating copy to clipboard operation
dbf copied to clipboard

Add as_dict method to dbf.Record

Open turicas opened this issue 1 year ago • 1 comments

I'd like to propose adding a as_dict method to dbf.Record class. It can be helpful if we don't know in advance the field names for a DBF and don't want to check table.field_names and cleanup strings.

I can create a PR with docs but already tested the following code:

    def as_dict(self, strip_strings=False):
        row = {}
        for field_name in self._meta.fields:
            value = self[field_name]
            if isinstance(value, str) and strip_strings:
                value = value.strip()
            row[field_name] = value
        return row

strip_strings is very useful for me (I use it in almost everytime I'm reading a DBF), since the string values always have trailing spaces.

turicas avatar Nov 07 '23 17:11 turicas

open your tables like this dbf.Table(..., default_data_types={'C':dbf.Char}) to automatically strip the strings refer https://stackoverflow.com/a/58701351 for more info

Nikhil1920 avatar Jan 06 '24 20:01 Nikhil1920