Skip to content

Commit

Permalink
add support for null values in D, F and L parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
william-andre committed Jul 1, 2019
1 parent dc18d57 commit fe619c5
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions dbfread/field_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __repr__(self):
# Make sure the string starts with "b'" in
# "InvalidValue(b'value here')".
text = 'b' + text

return 'InvalidValue({})'.format(text)

class FieldParser:
Expand Down Expand Up @@ -91,13 +91,13 @@ def parseD(self, field, data):
try:
return datetime.date(int(data[:4]), int(data[4:6]), int(data[6:8]))
except ValueError:
if data.strip(b' 0') == b'':
if data.strip(b' 0\0') == b'':
# A record containing only spaces and/or zeros is
# a NULL value.
return None
else:
raise ValueError('invalid date {!r}'.format(data))

def parseF(self, field, data):
"""Parse float field and return float or None"""
# In some files * is used for padding.
Expand All @@ -119,7 +119,7 @@ def parseL(self, field, data):
return True
elif data in b'FfNn':
return False
elif data in b'? ':
elif data in b'? \0':
return None
else:
# Todo: return something? (But that would be misleading!)
Expand Down Expand Up @@ -162,7 +162,7 @@ def parseN(self, field, data):
Returns int, float or None if the field is empty.
"""
# In some files * is used for padding.
data = data.strip().strip(b'*')
data = data.strip().strip(b'*\0')

try:
return int(data)
Expand Down Expand Up @@ -211,7 +211,7 @@ def parseT(self, field, data):
return None
else:
return None

def parseY(self, field, data):
"""Parse currency field (Y) and return decimal.Decimal.
Expand Down

0 comments on commit fe619c5

Please sign in to comment.