How can I use quotes without escaping them using CSV?

Reedick, Andrew jr9445 at ATT.COM
Wed Apr 9 17:39:07 EDT 2008



> -----Original Message-----
> From: python-list-bounces+jr9445=att.com at python.org [mailto:python-
> list-bounces+jr9445=att.com at python.org] On Behalf Of jeffself
> Sent: Wednesday, April 09, 2008 5:11 PM
> To: python-list at python.org
> Subject: How can I use quotes without escaping them using CSV?
> 
> 
> If I put an escape character in, it works.  For example, if I use ~ as
> my escape character, my output looks like this:
> 0001[tab]Michael L. ~"Mick~" Jones[tab]189
> 
> I don't want that. If I don't include an escape character, it doesn't
> work.
> 
> 
> Here's my code:
> import sys
> import csv
> from readexcel import *
> 
> f = open("results.txt", 'wb')
> book = sys.argv[1]
> sheet = sys.argv[2]
> 
> xl = readexcel(book)
> sheetnames = xl.worksheets()
> 
> for s in sheetnames:
>     if s == sheet:
>         writer = csv.writer(f, delimiter='\t', quoting=csv.QUOTE_NONE)
>         for row in xl.getiter(s):
> 
>
writer.writerow((row['Precinct'],row['Candidate'],unicode(int(row['Vote
> s']))))
> f.close()
> 


The documentation is pretty, uhm, obtuse, but you also need to set
quotechar.

import sys
import csv

names = ['Michael L. "Mick" Jones', 'Vickie A. Meyers', 'John "Jack"
Smith']

writer = csv.writer(sys.stdout, delimiter='\t', quotechar='',
quoting=csv.QUOTE_NONE)
for i in names:
	writer.writerow(['a', i, 'b'])

output:
a       Michael L. "Mick" Jones b
a       Vickie A. Meyers        b
a       John "Jack" Smith       b



*****

The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential, proprietary, and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from all computers. GA621





More information about the Python-list mailing list