Skip to content

Commit d72723c

Browse files
committed
Return None when parsing (illegal) NULL attribute values in numeric and date fields, like those produced by QGIS
1 parent 202143c commit d72723c

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
@@ -482,18 +482,22 @@ def __record(self):
482482
continue
483483
elif typ == "N":
484484
value = value.replace(b('\0'), b('')).strip()
485+
value = value.replace(b('*'), b('')) # QGIS NULL is all '*' chars
485486
if value == b(''):
486-
value = 0
487+
value = None
487488
elif deci:
488489
value = float(value)
489490
else:
490491
value = int(value)
491492
elif typ == b('D'):
492-
try:
493-
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
494-
value = [y, m, d]
495-
except:
496-
value = value.strip()
493+
if value.count(b('0')) == len(value): # QGIS NULL is all '0' chars
494+
value = None
495+
else:
496+
try:
497+
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:8])
498+
value = [y, m, d]
499+
except:
500+
value = value.strip()
497501
elif typ == b('L'):
498502
value = (value in b('YyTt') and b('T')) or \
499503
(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)