-3

我有一个字符串列表并保存到 csv 文件

imagesList=['img/201507/27/e731.gif','img/201507/29/e771.png']
myCSV =csv.writer(open('data.csv', 'wb'))
myCSV.writerow([imagesList])

像这样打开csv文件:

"['img/201507/27/e731.gif', 'img/201507/29/e771.png']"

但我想得到这样的

"["img/201507/27/e731.gif", "img/201507/29/e771.png"]"

所以我改变了代码

import csv
imagesList=['img/201507/27/e731.gif','img/201507/29/e771.png']
imgList = []
for image in imagesList:
    image = '"'+image+'"'
    imgList.append(image)
myCSV =csv.writer(open('data1.csv', 'wb'))
myCSV.writerow([imgList])

但我得到:

"['""img/201507/27/e731.gif""', '""img/201507/29/e771.png""']"

那么如何删除单引号(')?

4

1 回答 1

0

我想您想强制引用作者编写的字段csv,您的答案在于Dialects and Formatting Parameters

试一试:

myCSV = csv.writer(
    open('data1.csv', 'wb'),
    quotechar='"',
    quoting=csv.QUOTE_ALL
)
myCSV.writerows([imgList])
于 2015-08-04T07:57:23.553 回答