How can I use quotes without escaping them using CSV?

jeffself jeff.self at gmail.com
Wed Apr 9 19:37:11 EDT 2008


On Apr 9, 5:39 pm, "Reedick, Andrew" <jr9... at ATT.COM> wrote:
> > -----Original Message-----
> > From: python-list-bounces+jr9445=att.... at python.org [mailto:python-
> > list-bounces+jr9445=att.... at python.org] On Behalf Of jeffself
> > Sent: Wednesday, April 09, 2008 5:11 PM
> > To: python-l... 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

I tried this but a get the following error:
>>> writer = csv.writer(sys.stdout, delimiter='\t', quotechar=", quoting=csv.QUOTE_NONE)
  File "<stdin>", line 1
    writer = csv.writer(sys.stdout, delimiter='\t', quotechar=",
quoting=csv.QUOTE_NONE)
 
^
SyntaxError: EOL while scanning single-quoted string



More information about the Python-list mailing list