{"id":8026,"date":"2015-11-21T13:43:41","date_gmt":"2015-11-21T13:43:41","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/python-scrapy-how-to-get-csvitemexporter-to-write-columns-in-a-specific-order-open-source-projects-scrapy-scrapy\/"},"modified":"2022-08-30T15:03:03","modified_gmt":"2022-08-30T15:03:03","slug":"python-scrapy-how-to-get-csvitemexporter-to-write-columns-in-a-specific-order-open-source-projects-scrapy-scrapy","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/python-scrapy-how-to-get-csvitemexporter-to-write-columns-in-a-specific-order-open-source-projects-scrapy-scrapy\/","title":{"rendered":"Python Scrapy: How to get CSVItemExporter to write columns in a specific order-open source projects scrapy\/scrapy"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/68562e512e7e0efd1368c51aacd0cca1?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>gvb<\/strong><\/p>\n<p>This is related to Modifiying CSV export in scrapy<\/p>\n<p>The problem is that the exporter is instantiated without any keyword parameters, so the keywords like EXPORT_FIELDS are ignored. The solution is the same: you need to subclass the CSV item exporter to pass the keyword parameters.<\/p>\n<p>Following the above recipe, I created a new file xyzzy\/feedexport.py (change &#8220;xyzzy&#8221; to whatever your scrapy class is named):<\/p>\n<pre><code>\"\"\"\nThe standard CSVItemExporter class does not pass the kwargs through to the\nCSV writer, resulting in EXPORT_FIELDS and EXPORT_ENCODING being ignored\n(EXPORT_EMPTY is not used by CSV).\n\"\"\"\n\nfrom scrapy.conf import settings\nfrom scrapy.contrib.exporter import CsvItemExporter\n\nclass CSVkwItemExporter(CsvItemExporter):\n\n    def __init__(self, *args, **kwargs):\n        kwargs['fields_to_export'] = settings.getlist('EXPORT_FIELDS') or None\n        kwargs['encoding'] = settings.get('EXPORT_ENCODING', 'utf-8')\n\n        super(CSVkwItemExporter, self).__init__(*args, **kwargs)\n<\/code><\/pre>\n<p>and then added it into xyzzy\/settings.py:<\/p>\n<pre><code>FEED_EXPORTERS = {\n    'csv': 'xyzzy.feedexport.CSVkwItemExporter'\n}\n<\/code><\/pre>\n<p>Now the CSV exporter will honor the EXPORT_FIELD setting &#8211; also add to xyzzy\/settings.py:<\/p>\n<pre><code># By specifying the fields to export, the CSV export honors the order\n# rather than using a random order.\nEXPORT_FIELDS = [\n    'field1',\n    'field2',\n    'field3',\n]\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>gvb This is related to Modifiying CSV export in scrapy The problem is that the exporter is instantiated without any keyword parameters, so the keywords like EXPORT_FIELDS are ignored. The solution is the same: you need to subclass the CSV item exporter to pass the keyword parameters. Following the above recipe, I created a new [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8026","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8026","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8026"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8026\/revisions"}],"predecessor-version":[{"id":8684,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8026\/revisions\/8684"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8026"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}