superset icon indicating copy to clipboard operation
superset copied to clipboard

CSV_EXPORT.encoding config has no effect in Python3

Open georgexsh opened this issue 8 years ago • 2 comments

Superset version

Superset 0.26.3 and current HEAD 6b15592 Python 3.5.3 (via docker)

Expected results

after added following config:

CSV_EXPORT = {
    'encoding': 'utf-8-sig',
}

expected the exported csv file is encoded in utf-8-sig

Actual results

the exported CSV file is still encoded in utf-8, no BOM.

Steps to reproduce

set this option, export a CSV file in SQL lab.

related with #4506

Root cause

superset exports csv with following code:

csv = df.to_csv(index=False, **config.get('CSV_EXPORT'))

pandas.DataFrame.to_csv doc says:

path_or_buf : string or file handle, default None File path or object, if None is provided the result is returned as a string.

as superset has not set path_or_buf parameter, pandas use StringIO internally, in Python3, encoding parameter is omitted:

>>> df = pd.DataFrame(['中文', '测试'])
>>> r1 = df.to_csv()
>>> r2 = df.to_csv(encoding='utf-8-sig')
>>> r1 == r2
True
>>> type(r1), type(r2)
(<class 'str'>, <class 'str'>)

superset should do the encoding itself, in python3:

--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -2658,6 +2658,7 @@ class Superset(BaseSupersetView):
             df = query.database.get_df(sql, query.schema)
             # TODO(bkyryliuk): add compression=gzip for big files.
             csv = df.to_csv(index=False, **config.get('CSV_EXPORT'))
+        csv = csv.encode(config.get('CSV_EXPORT')['encoding'])
         response = Response(csv, mimetype='text/csv')
         response.headers['Content-Disposition'] = (
             'attachment; filename={}.csv'.format(unidecode(query.name)))

georgexsh avatar Jul 11 '18 08:07 georgexsh

nice!

wutengyue avatar Jul 11 '18 08:07 wutengyue

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. For admin, please label this issue .pinned to prevent stale bot from closing the issue.

stale[bot] avatar Apr 10 '19 18:04 stale[bot]