diff --git a/commcare_export/cli.py b/commcare_export/cli.py index 08d494b4..99b743bf 100644 --- a/commcare_export/cli.py +++ b/commcare_export/cli.py @@ -109,17 +109,13 @@ def main_with_args(args): # Reads as excel if it is a file name that looks like excel, otherwise reads as JSON, # falling back to parsing arg directly as JSON, and finally parsing stdin as JSON - missing_value = args.missing_value - if args.output_format != 'sql' and missing_value is None: - missing_value = '' - if args.query: if os.path.exists(args.query): query_file_md5 = misc.digest_file(args.query) if os.path.splitext(args.query)[1] in ['.xls', '.xlsx']: import openpyxl workbook = openpyxl.load_workbook(args.query) - query = excel_query.compile_workbook(workbook, missing_value) + query = excel_query.compile_workbook(workbook, args.missing_value) else: with io.open(args.query, encoding='utf-8') as fh: query = MiniLinq.from_jvalue(json.loads(fh.read())) diff --git a/commcare_export/writers.py b/commcare_export/writers.py index 644dea9d..9c41391a 100644 --- a/commcare_export/writers.py +++ b/commcare_export/writers.py @@ -12,7 +12,11 @@ MAX_COLUMN_SIZE = 2000 -def ensure_text(v): + +def ensure_text(v, convert_none=False): + if v is None: + return '' if convert_none else v + if isinstance(v, six.text_type): return v elif isinstance(v, six.binary_type): @@ -21,12 +25,13 @@ def ensure_text(v): return v.strftime('%Y-%m-%d %H:%M:%S') elif isinstance(v, datetime.date): return v.isoformat() - elif v is None: - return '' else: return u(str(v)) def to_jvalue(v): + if v is None: + return None + if isinstance(v, (six.text_type,) + six.integer_types): return v elif isinstance(v, six.binary_type): @@ -168,7 +173,7 @@ def write_table(self, table): self.output_stream.write('|%s|\n' % '|'.join(table['headings'])) for row in table['rows']: - self.output_stream.write('|%s|\n' % '|'.join(ensure_text(val) for val in row)) + self.output_stream.write('|%s|\n' % '|'.join(ensure_text(val, convert_none=True) for val in row)) class SqlMixin(object):