-3

I hava get a list of string and save to csv file

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

open csv file like this:

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

but what I want to get like this

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

so I change the code

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])

but I get:

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

so how can I delete the single quotes(')?

ivae
  • 37
  • 7
  • 4
    Why do you need double quotes? Why are you writing Python list notation into a CSV file in the first place? Why not use `myCSV.writerow(imgList)` (note, no brackets!). – Martijn Pieters Aug 04 '15 at 07:51
  • You should do `myCSV.writerows([imgList])` – Ozgur Vatansever Aug 04 '15 at 07:54
  • 1
    @ozgur: or just not wrap the list in another list. – Martijn Pieters Aug 04 '15 at 07:56
  • 2
    If you really want to get the desired output you should *not* use the `csv` module. That output is invalid CSV data, and no CSV parser will be able to read it. I suspect you perhaps want to produce JSON data, in which case the line should look like `["img/201507/27/e731.gif", "img/201507/29/e771.png"]` (no outer quotes) and you should use the `json` module to produce it. – Martijn Pieters Aug 04 '15 at 07:57
  • because I need upload a csv file to a website and it can only recognition double-quotes. – ivae Aug 04 '15 at 08:03
  • 1
    @ivae: but that website surely does *not* need the `"[` at the start and the `]"` at the end? – Martijn Pieters Aug 04 '15 at 08:04
  • Need,I mean that the website only can recognition `"["img/201507/27/e731.gif","img/201507/29/e771.png"]"` not `"['img/201507/27/e731.gif','img/201507/29/e771.png']"` – ivae Aug 04 '15 at 08:10
  • That's weird. What's the URL? – PM 2Ring Aug 04 '15 at 08:18
  • I want to write a android app ,so I use Baas as a Server like [Parse](https://www.parse.com/),I use Scrapy crawl some data save as csv file and import to the website,but the website can not recognition ,so I ask this question – ivae Aug 04 '15 at 08:29
  • 2
    @ivae seems like you want json data , not csv, you should use `import json; json.dump(myList,open('data.csv', 'w'))` – Anand S Kumar Aug 04 '15 at 08:37

1 Answers1

0

I suppose you want to enforce the quoting of the fields written by the csv writer, you your answer lies in the Dialects and Formatting Parameters.

Give a try with:

myCSV = csv.writer(
    open('data1.csv', 'wb'),
    quotechar='"',
    quoting=csv.QUOTE_ALL
)
myCSV.writerows([imgList])
bufh
  • 3,153
  • 31
  • 36
  • 1
    No amount of quote enforcement is going to do what the OP wants. They are writing the output of `repr(imgList)` into one CSV column. – Martijn Pieters Aug 04 '15 at 07:57
  • That's why I said "I suppose you want" when tried to guess what OP wanted from the original topic "change single quotes to double quotes"; I though the goal was to ensure each cell were to be quoted. You are being harsh down-voting for that much, but it's my loss, i suppose i'll STFU next time i'm unsure about a question ;^) – bufh Aug 04 '15 at 08:29
  • 1
    Good try, but best to clarify stuff in comments :) – Rob Grant Aug 04 '15 at 08:43
  • 2
    It can be tricky answering appropriately when the OP has an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – PM 2Ring Aug 04 '15 at 08:49