Arranging One Items Per One Column In A Row Of Csv File In Scrapy Python
I had items that scraped from a site which i placed them in to json files like below { 'author': ['TIM ROCK'], 'book_name': ['Truk Lagoon, Pohnpei & Kosrae Dive Guide'],
Solution 1:
The gist is this is very simple with csv.DictWriter
:
>>>inputs = [{..."author": ["TIM ROCK"], ..."book_name": ["Truk Lagoon, Pohnpei & Kosrae Dive Guide"], ..."category": "Travel", ...},...{..."author": ["JOY"], ..."book_name": ["PARSER"], ..."category": "Accomp", ...}...]>>>>>>from csv import DictWriter>>>from cStringIO import StringIO>>>>>>buf=StringIO()>>>c=DictWriter(buf, fieldnames=['author', 'book_name', 'category'])>>>c.writeheader()>>>c.writerows(inputs)>>>print buf.getvalue()
author,book_name,category
['TIM ROCK'],"['Truk Lagoon, Pohnpei & Kosrae Dive Guide']",Travel
['JOY'],['PARSER'],Accomp
It would be better to join those arrays on something, but since elements can be a list or a string, it's a bit tricky. Telling if something is a string or some-other-iterable is one of the few cases in Python where direct type-checking makes good sense.
>>>for row in inputs:...for k, v in row.iteritems():...ifnotisinstance(v, basestring):...try:... row[k] = ', '.join(v)...except TypeError:...pass... c.writerow(row)...>>>print buf.getvalue()
author,book_name,category
TIM ROCK,"Truk Lagoon, Pohnpei & Kosrae Dive Guide",Travel
JOY,PARSER,Accomp
Post a Comment for "Arranging One Items Per One Column In A Row Of Csv File In Scrapy Python"