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

error occured when parsing a 'N' type field which is mistakenly defined as ‘C' #41

Open
AlanGuoo opened this issue Dec 19, 2019 · 0 comments

Comments

@AlanGuoo
Copy link

hi,

When I was trying to read a dbf file, I found that when the dbf file has a wrong field type definition, it cannot read the data correctly.
e.g.
the blfm2 is actually filled with float, but it was mistakenly defined as 'C' type. If correctly parsed with the follwing method (dbf.py line 122)
self._read_field_headers(infile)

def _read_field_headers(self, infile):
        while True:
            sep = infile.read(1)
            if sep in (b'\r', b'\n', b''):
                # End of field headers
                break

            field = DBFField.unpack(sep + infile.read(DBFField.size - 1))

            field.type = chr(ord(field.type))

            # For character fields > 255 bytes the high byte
            # is stored in decimal_count.
            if field.type in 'C':
                field.length |= field.decimal_count << 8
                field.decimal_count = 0

            # Field name is b'\0' terminated.
            field.name = self._decode_text(field.name.split(b'\0')[0])
            if self.lowernames:
                field.name = field.name.lower()

            self.field_names.append(field.name)

            self.fields.append(field)

it should produce DBFField like:
DBFField(name='blfm2', type='C', address=196, length=17, decimal_count=12,

But the it turns out to be

DBFField(name='blfm2', type='C', address=196, length=3072, decimal_count=0

I think the problem is here:

            # For character fields > 255 bytes the high byte
            # is stored in decimal_count.
            if field.type in 'C':
                field.length |= field.decimal_count << 8
                field.decimal_count = 0

After I removed these lines, I finally got the correct result.

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

1 participant