From c55ff110a34d39be27bbd3d03fbf52caa271b4c9 Mon Sep 17 00:00:00 2001 From: stummjr Date: Wed, 27 Jan 2016 15:43:17 -0200 Subject: [PATCH] Fix CSV exporter for non string Python types. --- scrapy/exporters.py | 2 +- tests/test_exporters.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/scrapy/exporters.py b/scrapy/exporters.py index c7c78d054..55d74332b 100644 --- a/scrapy/exporters.py +++ b/scrapy/exporters.py @@ -200,7 +200,7 @@ class CsvItemExporter(BaseItemExporter): try: yield to_native_str(s) except TypeError: - yield to_native_str(repr(s)) + yield to_native_str(str(s)) def _write_headers_and_set_fields_to_export(self, item): if self.include_headers_line: diff --git a/tests/test_exporters.py b/tests/test_exporters.py index 662f8ec5c..97c09a495 100644 --- a/tests/test_exporters.py +++ b/tests/test_exporters.py @@ -271,6 +271,21 @@ class CsvItemExporterTest(BaseItemExporterTest): expected='"[4, 8]",John\r\n', ) + def test_other_python_types_item(self): + from datetime import datetime + now = datetime(2015, 1, 1, 1, 1, 1) + item = { + 'boolean': False, + 'number': 22, + 'time': now, + 'float': 3.14, + } + self.assertExportResult( + item=item, + include_headers_line=False, + expected='22,False,3.14,2015-01-01 01:01:01\r\n' + ) + class XmlItemExporterTest(BaseItemExporterTest):