Reputation: 37
I use python(2.7) Scrapy to crawl some data ,I want to save items as a csv file,this is my code:
class RenthouseinfoPipeline(object):
def __init__(self):
self.myCSV = csv.writer(open('data.csv', 'wb'))
self.myCSV.writerow(['int', 'string', 'string', 'string', 'string', 'string', 'string','array'])
self.myCSV.writerow(['rentPrice','houseType','floor','rentMode','paymentType','address','telphone','imagesList'])
def process_item(self, item, spider):
self.myCSV.writerow([item['rentPrice'].encode('utf-8'),item['houseType'].encode('utf-8'),item['floor'].encode('utf-8'),
item['rentMode'].encode('utf-8'),item['paymentType'].encode('utf-8'),item['address'].encode('utf-8'),
item['telphone'].encode('utf-8'),item['imagesList']])
return item
open csv file I get imagesList like this:
"[u'/rimg_458x358/uploads/img/201507/27/e731bd0378326f57dfedbb8d9a4e9016.gif', u'/rimg_458x358/uploads/img/201507/27/0bd27344f29a6041d8e96c74e3f5d332.jpg', u'/rimg_458x358/uploads/img/201507/27/784b90acfcc0e76e34fec21b0beab5b4.jpg', u'/rimg_458x358/uploads/img/201507/27/86e9f0492c5aadbe75dca31e4d2b17a1.jpg', u'/rimg_458x358/uploads/img/201507/27/e381583d26b9f9cf6dda84fe5378837e.jpg']"
but the Server can't accept this format! how can I get:single quotes to double quotes,and no prefix 'u' ?like this:
"["/rimg_458x358/uploads/img/201507/27/e731bd0378326f57dfedbb8d9a4e9016.gif", "/rimg_458x358/uploads/img/201507/27/0bd27344f29a6041d8e96c74e3f5d332.jpg", "/rimg_458x358/uploads/img/201507/27/784b90acfcc0e76e34fec21b0beab5b4.jpg", "/rimg_458x358/uploads/img/201507/27/86e9f0492c5aadbe75dca31e4d2b17a1.jpg", "/rimg_458x358/uploads/img/201507/27/e381583d26b9f9cf6dda84fe5378837e.jpg"]"
the progrom have no error,I only want to change the format!I just upload csv file to Server by a Webpage not pass by program
Upvotes: 0
Views: 518
Reputation: 373
Seems like your defaults are changed some where.
Try Using this for the the writer:
self.CSV = csv.writer(open('data.csv', 'wb'), delimiter = ',' , quotchar = """ )
That should work, but have not tested.
Upvotes: 1