Skip to content

Commit 0e5de22

Browse files
Merge pull request #16 from riggsd/null-friendly
Gracefully handle NULL numeric, date fields
2 parents be538eb + d72723c commit 0e5de22

File tree

4 files changed

+10
-6
lines changed

4 files changed

+10
-6
lines changed

shapefile.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -493,18 +493,22 @@ def __record(self):
493493
continue
494494
elif typ == "N":
495495
value = value.replace(b('\0'), b('')).strip()
496+
value = value.replace(b('*'), b('')) # QGIS NULL is all '*' chars
496497
if value == b(''):
497-
value = 0
498+
value = None
498499
elif deci:
499500
value = float(value)
500501
else:
501502
value = int(value)
502503
elif typ == b('D'):
503-
try:
504-
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
505-
value = [y, m, d]
506-
except:
507-
value = value.strip()
504+
if value.count(b('0')) == len(value): # QGIS NULL is all '0' chars
505+
value = None
506+
else:
507+
try:
508+
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
509+
value = [y, m, d]
510+
except:
511+
value = value.strip()
508512
elif typ == b('L'):
509513
value = (value in b('YyTt') and b('T')) or \
510514
(value in b('NnFf') and b('F')) or b('?')

shapefiles/test/NullTest.dbf

550 Bytes
Binary file not shown.

shapefiles/test/NullTest.shp

184 Bytes
Binary file not shown.

shapefiles/test/NullTest.shx

124 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)