From 2dfdde3c79a5be468302a1e825cc5ad77444a8ac Mon Sep 17 00:00:00 2001 From: Elias Dorneles Date: Mon, 25 Jan 2016 22:24:35 -0200 Subject: [PATCH] fallback to repr when can't convert to native string --- scrapy/exporters.py | 11 +++++++++-- tests/test_exporters.py | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/scrapy/exporters.py b/scrapy/exporters.py index fa6663ed4..69c180ea4 100644 --- a/scrapy/exporters.py +++ b/scrapy/exporters.py @@ -192,9 +192,16 @@ class CsvItemExporter(BaseItemExporter): fields = self._get_serialized_fields(item, default_value='', include_empty=True) - values = [to_native_str(x) for _, x in fields] + values = list(self._build_row(x for _, x in fields)) self.csv_writer.writerow(values) + def _build_row(self, values): + for s in values: + try: + yield to_native_str(s) + except TypeError: + yield to_native_str(repr(s)) + def _write_headers_and_set_fields_to_export(self, item): if self.include_headers_line: if not self.fields_to_export: @@ -204,7 +211,7 @@ class CsvItemExporter(BaseItemExporter): else: # use fields declared in Item self.fields_to_export = list(item.fields.keys()) - row = [to_native_str(s) for s in self.fields_to_export] + row = list(self._build_row(self.fields_to_export)) self.csv_writer.writerow(row) diff --git a/tests/test_exporters.py b/tests/test_exporters.py index 61a0229a4..8930545a6 100644 --- a/tests/test_exporters.py +++ b/tests/test_exporters.py @@ -237,6 +237,13 @@ class CsvItemExporterTest(BaseItemExporterTest): expected='"Mary,Paul",John\r\n', ) + def test_join_multivalue_not_strings(self): + self.assertExportResult( + item=dict(name='John', friends=[4, 8]), + include_headers_line=False, + expected='"[4, 8]",John\r\n', + ) + class XmlItemExporterTest(BaseItemExporterTest):