Skip to content

Commit

Permalink
PR #120 from @sebastic - empty string None option in CsvFileInput
Browse files Browse the repository at this point in the history
Add CsvFileInput option to use None instead of empty string.
  • Loading branch information
justb4 authored Nov 24, 2021
2 parents 82c1418 + c8b9139 commit 009e39f
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions stetl/inputs/fileinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,13 @@ def quote_char(self):
"""
pass

@Config(ptype=bool, default=False, required=False)
def empty_string_is_none(self):
"""
Should we use None instead of '' for empty fields
"""
pass

# Constructor
def __init__(self, configdict, section):
FileInput.__init__(self, configdict, section, produces=[FORMAT.record_array, FORMAT.record])
Expand All @@ -381,11 +388,26 @@ def read(self, packet):
try:
# To comply with Stetl record type: force ordinary/base dict-type.
# Python 3.6+ returns OrderedDict which may not play nice up the Chain
packet.data = dict(next(self.csv_reader))
record = dict(next(self.csv_reader))

if self.empty_string_is_none:
for field in record:
if record[field] == '':
record[field] = None

packet.data = record
if self._output_format == FORMAT.record_array:
while True:
self.arr.append(packet.data)
packet.data = dict(next(self.csv_reader))

record = dict(next(self.csv_reader))

if self.empty_string_is_none:
for field in record:
if record[field] == '':
record[field] = None

packet.data = record

log.info("CSV row nr %d read: %s" % (self.csv_reader.line_num - 1, packet.data))
except Exception:
Expand Down

0 comments on commit 009e39f

Please sign in to comment.