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

add support for null values in D, F and L parsing #35

Merged
merged 1 commit into from
Dec 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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