CSV Dialect Example Requested - NEWBIE

C

caldwellinva

Hi!

** CSV Dialect Example Requested - NEWBIE **

I would like to use the CSV module and would like to see a working example
that uses a custom dialect. I have tried the following example from an
earlier posting to the group

import csv

import csv
class dialect(csv.dialect):
delimiter = '|'
skipinitialspace = True
quotechar = '"'
doublequote = True

reader = csv.reader(file("c:\\Temp\\test.csv"), dialect=dialect)
for row in reader:
print row

and get the following error

Traceback (most recent call last):
File
"C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python23\DougSource\test_csv.py", line 4, in ?
# delimeter = '|'
AttributeError: 'module' object has no attribute 'dialect'

If the example could show the process for registering a name, that would be
helpful as well.

Thank you,

Doug Caldwell
5533 Dunsmore Road
Alexandria, VA 22315

(e-mail address removed)
703-922-8725
 
V

Vincent Marchetti

"caldwellinva" said:
Hi!

** CSV Dialect Example Requested - NEWBIE **

I would like to use the CSV module and would like to see a working example
that uses a custom dialect. I have tried the following example from an
earlier posting to the group

import csv

import csv
class dialect(csv.dialect):
delimiter = '|'
skipinitialspace = True
quotechar = '"'
doublequote = True

I do not have an example, but the first failure in this script is that the
class in the csv module that you want to use as a base class is
csv.Dialect (note case). This causes the attribute error you report.

Vince
 
C

caldwellinva

Hi!
I do not have an example, but the first failure in this script is that the
class in the csv module that you want to use as a base class is
csv.Dialect (note case). This causes the attribute error you report.

Vince, thank you for pointing this out ... it runs further now, but still
produces an error.

Revised Version

import csv

import csv
class dialect(csv.Dialect):
delimiter = '|'
skipinitialspace = True
quotechar = '"'
doublequote = True

reader = csv.reader(file("c:\\Temp\\test.csv"), dialect=dialect)
for row in reader:
print row

Error Message

Traceback (most recent call last):
File
"C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python23\DougSource\test_csv.py", line 10, in ?
reader = csv.reader(file("c:\\Temp\\test.csv"), dialect=dialect)
File "C:\Python23\lib\csv.py", line 39, in __init__
raise Error, "Dialect did not validate: %s" % ", ".join(errors)
Error: Dialect did not validate: lineterminator not set, quoting parameter
not set

Thank you,

Doug Caldwell
5533 Dunsmore Road
Alexandria, VA 22315

(e-mail address removed)
703-922-8725
 
P

Peter Otten

caldwellinva said:
Hi!


Vince, thank you for pointing this out ... it runs further now, but still
produces an error.

Revised Version

import csv

import csv
class dialect(csv.Dialect):
delimiter = '|'
skipinitialspace = True
quotechar = '"'
doublequote = True

reader = csv.reader(file("c:\\Temp\\test.csv"), dialect=dialect)
for row in reader:
print row

Error Message

Traceback (most recent call last):
File
"C:\Python23\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Python23\DougSource\test_csv.py", line 10, in ?
reader = csv.reader(file("c:\\Temp\\test.csv"), dialect=dialect)
File "C:\Python23\lib\csv.py", line 39, in __init__
raise Error, "Dialect did not validate: %s" % ", ".join(errors)
Error: Dialect did not validate: lineterminator not set, quoting parameter
not set

You need these additional attributes - just read the traceback :)

lineterminator = '\r\n' # or probably '\n' if not on Windows
quoting = csv.QUOTE_MINIMAL

By the way, the easiest way to get a working Dialect is to subclass
csv.excel, e. g:

class PipeDialect(csv.excel):
delimiter = '|'

Peter
 
S

Skip Montanaro

Doug> import csv

Doug> import csv
Doug> class dialect(csv.dialect):
Doug> delimiter = '|'
Doug> skipinitialspace = True
Doug> quotechar = '"'
Doug> doublequote = True

Doug> reader = csv.reader(file("c:\\Temp\\test.csv"), dialect=dialect)
Doug> for row in reader:
Doug> print row

Doug,

As others pointed out you want to subclass csv.Dialect. It's probably
easier to subclass csv.excel instead though. csv.Dialect is a base class
which doesn't define any of the required attributes. If you subclass
csv.Dialect using just the attributes you indicated, you will get:
... delimiter = '|'
... skipinitialspace = True
... quotechar = '"'
... doublequote = True
... Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/Users/skip/local/lib/python2.4/csv.py", line 39, in __init__
raise Error, "Dialect did not validate: %s" % ", ".join(errors)
_csv.Error: Dialect did not validate: lineterminator not set, quoting parameter not set

because csv.Dialect doesn't define a lineterminator attribute. By
subclassing csv.excel you start out with a valid dialect and only need to
override the attributes which differ.

Here's a working example:
... delimiter = '|'
... skipinitialspace = True
... quotechar = '"'
... doublequote = True
... ... print row
...
['"', '1', '2', '3', '4']
['"Happy Days"', 'a', 'b', 'c', '4']

which uses

""""|1|2|3|"4"
"""Happy Days"""|a|b|c|"4"

as the input file.

Here's an example of registering a dialect with the module:
... print row
...
['"', '1', '2', '3', '4']
['"Happy Days"', 'a', 'b', 'c', '4']

Skip
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top