Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add as_dict method to dbf.Record #42

Open
turicas opened this issue Nov 7, 2023 · 1 comment
Open

Add as_dict method to dbf.Record #42

turicas opened this issue Nov 7, 2023 · 1 comment

Comments

@turicas
Copy link

turicas commented Nov 7, 2023

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.

@Nikhil1920
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants