zhikrullah / pyshp

Automatically exported from code.google.com/p/pyshp
MIT License
0 stars 0 forks source link

ValueError when a dbf record has a number with 0 decimals #53

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
The __record function tries an integer conversion, when deci == 0.
def __record(self):
    """Reads and returns a dbf record row as a list of values."""
    f = self.__getFileObj(self.dbf)
    recFmt = self.__recordFmt()
    recordContents = unpack(recFmt[0], f.read(recFmt[1]))
    if recordContents[0] != b(' '):
        # deleted record
        return None
    record = []
    for (name, typ, size, deci), value in zip(self.fields,
                                                                                            recordContents):
        if name == 'DeletionFlag':
            continue
        elif not value.strip():
            record.append(value)
            continue
        elif typ == "N":
            value = value.replace(b('\0'), b('')).strip()
            if value == b(''):
                value = 0
            elif deci:
                value = float(value)
            else:
                value = int(value)  # This is the problem
        elif typ == b('D'):
            try:
                y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
                value = [y, m, d]
            except:
                value = value.strip()
        elif typ == b('L'):
            value = (value in b('YyTt') and b('T')) or \
                                    (value in b('NnFf') and b('F')) or b('?')
        else:
            value = u(value)
            value = value.strip()
        record.append(value)
    return record

This, however, is not always acceptable, as the number may also be a float with 
zero decimals. E.g. int("1234.") fails on ValueError (invalid literal for int() 
with base 10: '1234.').

Original issue reported on code.google.com by Juuso.me...@gmail.com on 6 Aug 2013 at 7:30

GoogleCodeExporter commented 8 years ago
A suggested patch for the error attached.

Original comment by Juuso.me...@gmail.com on 6 Aug 2013 at 7:41

Attachments: