ordering fields return by yield

47 views
Skip to first unread message

Raf Roger

unread,
Aug 27, 2016, 3:02:39 PM8/27/16
to scrapy-users
Hi,

i have a code line as following:
yield {
      "count" : self.counter,
      "page number" : self.page_num,
      "company page url" : response.url,
      "company" : name,
      "Street" : street,
      "zip code" : zipcode,
      "city" : city,
      "country" : country,
      "web" : web
    }

However in the csv result, fields do not respect this order and therefore the "web" field can be as first field in csv export and next can be "zip code"

How to force it to have order respected ?

thx.

paul8...@gmail.com

unread,
Sep 14, 2016, 3:33:00 AM9/14/16
to scrapy-users


在 2016年8月28日星期日 UTC+8上午3:02:39,Raf Roger写道:
在 2016年8月28日星期日 UTC+8上午3:02:39,Raf Roger写道:

Erik Dominguez

unread,
Nov 4, 2016, 10:06:44 PM11/4/16
to scrapy-users
It is a Dict, keys are never going to be in ordered when you iterate through them. You need to use an OrderedDict

from collections import OrderedDict


yield OrderedDict({
     "count" : self.counter,
     "page number" : self.page_num,
     "company page url" : response.url,
     "company" : name,
     "Street" : street,
     "zip code" : zipcode,
     "city" : city,
     "country" : country,
     "web" : web
   })


Reply all
Reply to author
Forward
0 new messages