简体   繁体   中英

How to format Scrapy exports

I currently store my scraped data in an item to export in .csv format:

item = HobbyItem()
item['name'] = user.getName()
item['hobbies'] = user.getHobbies()

The item is declared as follows:

name = scrapy.Field()
hobbies = scrapy.Field()

This results in a csv format as follows:

name,hobbies
Tim, [['tennis'],['squash'],['music']]
Bob, [['rugby'],['polo']]

What I actually want is this:

name,hobbies
Tim, 'tennis'
Tim, 'squash'
Tim, 'music'
Bob, 'rugby'
Bob, 'polo'

Does anybody know how I can modify the output to achieve this?

You did not let enough code or explanation for me to be sure that this will work in your particular code but

Try :

for element in user.getHobbies():
    item['name'] = user.getName()
    item['hobbies'] = element[0]

This will create 1 element per hobby, using getName multiple times

name = user.getName()
for hobby in user.getHobbies():
    if hobby:
        item = HobbyItem()
        item['name'] = name
        item['hobbies'] = hobby[0]
        yield item

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM