dbf
dbf copied to clipboard
Add as_dict method to dbf.Record
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.
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