Skip to content

Commit

Permalink
Merge pull request #65 from dimagi/sk/None
Browse files Browse the repository at this point in the history
write None to excel
  • Loading branch information
snopoke committed Nov 23, 2017
2 parents 8ec3932 + a7830db commit f4c22b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
6 changes: 1 addition & 5 deletions commcare_export/cli.py
Expand Up @@ -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()))
Expand Down
13 changes: 9 additions & 4 deletions commcare_export/writers.py
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit f4c22b2

Please sign in to comment.