csv module strangeness.

tobiah st at tobiah.org
Wed Aug 30 16:31:11 EDT 2006


I'm trying to create a cvs.reader object using a custom dialect.

The docs are a little terse, but I gather that I am supposed
to subclass cvs.Dialect:

class dialect(csv.Dialect):
        pass

Now, the docs say that all of the attributes have reasonable
defaults, but instantiating the above gives:

Traceback (most recent call last):
  File "<stdin>", line 15, in ?
  File "/usr/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: delimiter character not set, quotechar not set, lineterminator not set, doublequote parameter must be True or False, skipinitialspace parameter must be True or False, quoting parameter not set

So I look at the source.  The Dialect class is very simple,
and starts with:

class Dialect:
    _name = ""
    _valid = False
    # placeholders
    delimiter = None
    quotechar = None
    escapechar = None
    doublequote = None
    skipinitialspace = None
    lineterminator = None
    quoting = None

So, it's no wonder that it fails its validate() call.
The only thing that I can think of to do is to set
these on the class itself before instantiation:

###############################################
import csv

class dialect(csv.Dialect):
        pass

dialect.delimiter = "\t"
dialect.quotechar = '"'
dialect.lineterminator = "\n"
dialect.doublequote = True
dialect.skipinitialspace = True
dialect.quoting = csv.QUOTE_MINIMAL

d = dialect()

reader = csv.reader(open('list.csv'))
for row in reader:
        print row
###############################################

This runs, but the delimiter is still the comma.
When list.csv is comma delim, it works correctly,
but when list.csv has tab separated values, I
get back a single field with the entire line in 
it.

I suppose I must be doing something horribly wrong.

Thanks,

Tobiah

-- 
Posted via a free Usenet account from http://www.teranews.com




More information about the Python-list mailing list