提交 0e7b2a6d 编写于 作者: D Daniel Grana

write header line by default when using csv exporter

--HG--
extra : rebase_source : 2d2d7153dde5e3f77e682e16d2e4408f732f234e
上级 c33d6bbd
...@@ -244,7 +244,7 @@ XmlItemExporter ...@@ -244,7 +244,7 @@ XmlItemExporter
CsvItemExporter CsvItemExporter
--------------- ---------------
.. class:: CsvItemExporter(file, include_headers_line=False, \**kwargs) .. class:: CsvItemExporter(file, include_headers_line=True, \**kwargs)
Exports Items in CSV format to the given file-like object. If the Exports Items in CSV format to the given file-like object. If the
:attr:`fields_to_export` attribute is set, it will be used to define the :attr:`fields_to_export` attribute is set, it will be used to define the
...@@ -254,9 +254,8 @@ CsvItemExporter ...@@ -254,9 +254,8 @@ CsvItemExporter
:param file: the file-like object to use for exporting the data. :param file: the file-like object to use for exporting the data.
:param include_headers_line: If enabled, makes the exporter output a header :param include_headers_line: If enabled, makes the exporter output a header
line with the field names taken from line with the field names taken from
:attr:`BaseItemExporter.fields_to_export` so that attribute must also be :attr:`BaseItemExporter.fields_to_export` or the first exported item fields.
set in order to work (otherwise it raises a :exc:`RuntimeError`)
:type include_headers_line: boolean :type include_headers_line: boolean
The additional keyword arguments of this constructor are passed to the The additional keyword arguments of this constructor are passed to the
...@@ -266,6 +265,7 @@ CsvItemExporter ...@@ -266,6 +265,7 @@ CsvItemExporter
A typical output of this exporter would be:: A typical output of this exporter would be::
product,price
Color TV,1200 Color TV,1200
DVD player,200 DVD player,200
......
...@@ -101,25 +101,29 @@ class XmlItemExporter(BaseItemExporter): ...@@ -101,25 +101,29 @@ class XmlItemExporter(BaseItemExporter):
class CsvItemExporter(BaseItemExporter): class CsvItemExporter(BaseItemExporter):
def __init__(self, file, include_headers_line=False, **kwargs): def __init__(self, file, include_headers_line=True, **kwargs):
self._configure(kwargs, dont_fail=True) self._configure(kwargs, dont_fail=True)
self.include_headers_line = include_headers_line self.include_headers_line = include_headers_line
self.csv_writer = csv.writer(file, **kwargs) self.csv_writer = csv.writer(file, **kwargs)
self._header_written = False
def start_exporting(self):
if self.include_headers_line:
if not self.fields_to_export:
raise RuntimeError("You must set fields_to_export in order" + \
" to use include_headers_line")
self.csv_writer.writerow(self.fields_to_export)
def export_item(self, item): def export_item(self, item):
if not self._header_written:
self._write_header(item)
fields = self._get_serialized_fields(item, default_value='', \ fields = self._get_serialized_fields(item, default_value='', \
include_empty=True) include_empty=True)
values = [x[1] for x in fields] values = [x[1] for x in fields]
self.csv_writer.writerow(values) self.csv_writer.writerow(values)
def _write_header(self, item):
self._header_written = True
if self.include_headers_line:
if not self.fields_to_export:
self.fields_to_export = item.fields.keys()
self.csv_writer.writerow(self.fields_to_export)
class PickleItemExporter(BaseItemExporter): class PickleItemExporter(BaseItemExporter):
......
...@@ -95,20 +95,38 @@ class CsvItemExporterTest(BaseItemExporterTest): ...@@ -95,20 +95,38 @@ class CsvItemExporterTest(BaseItemExporterTest):
return CsvItemExporter(self.output, **kwargs) return CsvItemExporter(self.output, **kwargs)
def _check_output(self): def _check_output(self):
self.assertEqual(self.output.getvalue(), '22,John\xc2\xa3\r\n') self.assertEqual(self.output.getvalue(), 'age,name\r\n22,John\xc2\xa3\r\n')
def test_header(self): def test_header(self):
output = StringIO() output = StringIO()
ie = CsvItemExporter(output, include_headers_line=True) ie = CsvItemExporter(output, fields_to_export=self.i.fields.keys())
self.assertRaises(RuntimeError, ie.start_exporting)
ie = CsvItemExporter(output, include_headers_line=True, \
fields_to_export=self.i.fields.keys())
ie.start_exporting() ie.start_exporting()
ie.export_item(self.i) ie.export_item(self.i)
ie.finish_exporting() ie.finish_exporting()
self.assertEqual(output.getvalue(), 'age,name\r\n22,John\xc2\xa3\r\n') self.assertEqual(output.getvalue(), 'age,name\r\n22,John\xc2\xa3\r\n')
output = StringIO()
ie = CsvItemExporter(output, fields_to_export=['age'])
ie.start_exporting()
ie.export_item(self.i)
ie.finish_exporting()
self.assertEqual(output.getvalue(), 'age\r\n22\r\n')
output = StringIO()
ie = CsvItemExporter(output)
ie.start_exporting()
ie.export_item(self.i)
ie.export_item(self.i)
ie.finish_exporting()
self.assertEqual(output.getvalue(), 'age,name\r\n22,John\xc2\xa3\r\n22,John\xc2\xa3\r\n')
output = StringIO()
ie = CsvItemExporter(output, include_headers_line=False)
ie.start_exporting()
ie.export_item(self.i)
ie.finish_exporting()
self.assertEqual(output.getvalue(), '22,John\xc2\xa3\r\n')
class XmlItemExporterTest(BaseItemExporterTest): class XmlItemExporterTest(BaseItemExporterTest):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册