How do I put single-quotes around string using Python csv writer?

11.7k views Asked by At
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":

"[email protected]"
"[email protected]"
"[email protected]"

But I want the output to be

'[email protected]',
'[email protected]',
'[email protected]'

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?

4

There are 4 answers

0
alexis On

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" )
0
qmorgan On

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()
2
Simeon Visser On

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

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

3
Matt On

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.