How can I use quotes without escaping them using CSV?

J

jeffself

I'm reading data out of an Excel spreadsheet using the XLRD module.
The spreadsheet contains a list of election results. The fields are
as follows: Precinct, Candidate, Votes

The problem is candidate names can be funky, for instance: Michael L.
"Mick" Jones

I cannot for the life of me figure out how to get the CSV module to
allow a name like this to be written to a file. Why does it insist on
an escape character when I'm telling it that the delimiter should be
'\t'? I want the quotes to go to the file and I want the tab-
delimited file to look like this:

0001[tab]Michael L. "Mick" Jones[tab]189
0002[tab]Vickie A. Meyers[tab]221
0003[tab]John "Jack" Smith[tab]187

Note: I don't want [tab] to display, I want a real tab there.

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['Votes']))))
f.close()


Thanks!
 
R

Reedick, Andrew

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of jeffself
Sent: Wednesday, April 09, 2008 5:11 PM
To: (e-mail address removed)
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
 
J

John Machin

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of jeffself
Sent: Wednesday, April 09, 2008 5:11 PM
To: (e-mail address removed)
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.

which means an exception is raised:

_csv.Error: need to escape, but no escapechar set
Here's my code:
[snip]
writer = csv.writer(f, delimiter='\t', quoting=csv.QUOTE_NONE)
[snip]

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

Uhm, "obtuse" applies to angles and people :) I could agree with
"obscure".
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

*****

Here's my call:

(1) Code bug: when quoting is set to QUOTE_NONE, it should ignore the
setting of quotechar -- it's irrelevant.

(2) Documentation bug:
"""
quotechar
A one-character string ....
"""
but the code allows setting quotechar to '' and to None. Both work
around the OP's problem; whether they have identical effect in other
situations, I haven't explored.

Cheers,
John
 
J

jeffself

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of jeffself
Sent: Wednesday, April 09, 2008 5:11 PM
To: (e-mail address removed)
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: File "<stdin>", line 1
writer = csv.writer(sys.stdout, delimiter='\t', quotechar=",
quoting=csv.QUOTE_NONE)

^
SyntaxError: EOL while scanning single-quoted string
 
J

jeffself

-----Original Message-----
From: [email protected] [mailto:python-
[email protected]] On Behalf Of jeffself
Sent: Wednesday, April 09, 2008 5:11 PM
To: (e-mail address removed)
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 set quotechar="" and was able to get it to work. I'll try this at
work tomorrow!
 
J

John Machin

I set quotechar="" and was able to get it to work. I'll try this at
work tomorrow!

setting it to what Andrew told you to do ('' not ")works equally well
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top