python将列表保存到csv单引号

时间:2015-08-04 03:32:12

标签: python csv

我使用python(2.7)Scrapy来抓取一些数据,我想将项目保存为csv文件,这是我的代码:

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

打开csv文件我得到像这样的imagesList:

"[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']"

但服务器无法接受此格式!我怎么能得到:单引号加双引号,没有前缀'u'?像这样:

"["/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"]"

progrom没有错误,我只想更改格式!我只是通过网页上传csv文件到服务器

1 个答案:

答案 0 :(得分:1)

似乎你的默认值在某些地方被改变了。

尝试将此用于作者:

    self.CSV = csv.writer(open('data.csv', 'wb'), delimiter = ',' , quotchar = """ )

这应该有效,但尚未经过测试。