4
with open("emails.csv", "w") as csvfile:
    for control_rent_email in control_group_renters:
        email_writer = csv.writer(csvfile, quotechar='"', quoting=csv.QUOTE_NONNUMERIC,delimiter=',')
        email_writer.writerow([control_rent_email])
    csvfile.close()

I get the following output in the "emails.csv":

"susan@gmail.com"
"joe@gmail.com"
"sara@yahoo.com"

But I want the output to be

'susan@gmail.com',
'joe@gmail.com',
'sara@yahoo.com'

How do I put the correct parameters in csv.writer() to achieve this desired result?

UPDATE:

1)

email_writer = csv.writer(csvfile, quotechar=''', quoting=csv.QUOTE_NONNUMERIC,delimiter=',')                                                                                            
SyntaxError: EOL while scanning string literal

2) How do I place the "," to look like the output result?

user1947085
  • 177
  • 1
  • 4
  • 7

4 Answers4

3

Change your quote characters as noted by others or use a backslash escape.

   email_writer = csv.writer(csvfile, quotechar='\'', quoting=csv.QUOTE_NONNUMERIC,delimiter=',')

We seemed to have missed your second problem:

This is a hack because I can't seem to figure out how to do this with the csv module. Rather than writing your files using the csv.writer, you can try it this way:

for control_rent_email in control_group_renters:
    csvfile.write('\'' + control_rent_email + '\'' + ',')

You also don't need to explicitly close the file you're writing to as the way you're opening it does it when it isn't referenced anymore.

I hope someone out there can figure out how to do this in a less-hack sort of way, particularly using the csv.writer. This way will suit your needs though.

Matt
  • 3,508
  • 6
  • 38
  • 66
  • This seems right. Given the OP's requirements, using the csv module seems to be the real hack. – alexis Dec 07 '13 at 21:29
  • @alexis So it's not just me then? I searched and tested for a bit to see if it *was* possible but I couldn't come up with a solution other than what I put above. I would think the `csv` module would have something that would do this easily. – Matt Dec 07 '13 at 21:40
  • The CSV module is for producing variants of the CSV format. Did you notice that (s)he doesn't want a comma after the last line? And why force the quotes around strings that don't need to be quoted, but have a trailing comma? That's not CSV. You can do it by using `QUOTE_NONE`, inserting the surrounding quotes by hand (as you did above), and passing an empty string to trigger a trailing comma (see my answer). Your solution is far more appropriate, as I said. – alexis Dec 07 '13 at 21:44
1

A csv writer is a device you can use to write multiple lines into a file. You should only create one. Tell it to use a single quote as your quote character, and use it this way:

with open("emails.csv", "w") as csvfile:
    email_writer = csv.writer(csvfile, quotechar="'", quoting=csv.QUOTE_NONNUMERIC,delimiter=',')
    for control_rent_email in control_group_renters:
        email_writer.writerow([control_rent_email])

I hope that you're giving a simplified example of what you really need; otherwise, why go to all this trouble when you're only printing one string per line? If you really want a trailing comma on each line except the last, you're really not aiming for any sort of CSV and you should construct your outputs directly.

That said, you can get trailing commas by passing an empty string to writerow, but only if you use QUOTE_MINIMAL or QUOTE_NONE (which will suppress the pointless quotes around the emails), instead of QUOTE_NONNUMERIC; otherwise you'll get quotes around the empty strings.

email_writer.writerow([control_rent_email, ""])

For completeness, here's how you can really generate exactly the output you want, without the final comma and all:

with open("emails.txt", "w") as output:
    output.write( ",\n".join("'"+r+"'" for r in control_group_renters) +"\n" )
alexis
  • 48,685
  • 16
  • 101
  • 161
0

It looks to be your quotechar. Try:

with open("emails.csv", "w") as csvfile:
    for control_rent_email in control_group_renters:
        email_writer = csv.writer(csvfile, quotechar="'", quoting=csv.QUOTE_NONNUMERIC,delimiter=',')
        email_writer.writerow([control_rent_email])
    csvfile.close()
qmorgan
  • 4,794
  • 2
  • 19
  • 14
0

You should change the quote character to quotechar="'".

In your code you have explicitly specified that the quote character should be double quotes.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180