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?
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:
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 useQUOTE_MINIMAL
orQUOTE_NONE
(which will suppress the pointless quotes around the emails), instead ofQUOTE_NONNUMERIC
; otherwise you'll get quotes around the empty strings.For completeness, here's how you can really generate exactly the output you want, without the final comma and all: