2017-02-01 11:49:54 -05:00
|
|
|
import six
|
|
|
|
|
|
|
|
|
2017-01-04 10:47:00 -05:00
|
|
|
def csv_format(data):
|
|
|
|
"""
|
|
|
|
Encapsulate any data which contains a comma within double quotes.
|
|
|
|
"""
|
|
|
|
csv = []
|
2017-02-01 11:49:54 -05:00
|
|
|
for value in data:
|
|
|
|
|
|
|
|
# Represent None or False with empty string
|
|
|
|
if value in [None, False]:
|
2017-01-04 10:47:00 -05:00
|
|
|
csv.append(u'')
|
2017-02-01 11:49:54 -05:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Force conversion to string first so we can check for any commas
|
|
|
|
if not isinstance(value, six.string_types):
|
|
|
|
value = u'{}'.format(value)
|
|
|
|
|
|
|
|
# Double-quote the value if it contains a comma
|
|
|
|
if u',' in value:
|
|
|
|
csv.append(u'"{}"'.format(value))
|
2017-01-04 10:47:00 -05:00
|
|
|
else:
|
2017-02-01 11:49:54 -05:00
|
|
|
csv.append(u'{}'.format(value))
|
|
|
|
|
2017-01-04 10:47:00 -05:00
|
|
|
return u','.join(csv)
|