1

The documentation states that the default value of Dialect.delimiter is ','. However, when I try to print, it returns None.

>>> print(csv.Dialect.delimiter)
None

Dialect.delimiter A one-character string used to separate fields. It defaults to ','.

1
  • @Aran-Fey is there any possible explanation for this? Commented Sep 13, 2018 at 13:25

2 Answers 2

2

This is default value when you creating your own dialect.

import csv

csv.register_dialect('my_dialect')
dialect = csv.get_dialect('my_dialect')
print(dialect.delimiter)
Sign up to request clarification or add additional context in comments.

4 Comments

@Syzmon but I can also create a dialect by inheriting from Dialect class. In this case, however, I have to set every formatting arguments. Clearly, this is a conflict,
This only holds true when using register_dialect() function.
yes, but you have to register dialect before you will use it
No, you can assign the class object directly to the dialect argument.
2

That's the default value when you create a new Dialect. You can get a dialect instance (an inmutable one if you're using Python 3.x) with csv.get_dialect() and register your own using csv.register_dialect. You can also just subclass Dialect and get on with it.

The documentation may seem unclear if you just read that line, but a few lines above you can see that:

To make it easier to specify the format of input and output records, specific formatting parameters are grouped together into dialects. A dialect is a subclass of the Dialect class having a set of specific methods and a single validate() method.

Doing a simple help(csv.Dialect) in the REPL confirms that subclassing is a must when working with this class.

class Dialect(builtins.object)
 |  Describe a CSV dialect.
 |
 |  This must be subclassed (see csv.excel).  Valid attributes are:
 |  delimiter, quotechar, escapechar, doublequote, skipinitialspace,
 |  lineterminator, quoting.
 |
 |  Methods defined here:
 |
 |  __init__(self)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)
 |
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |
 |  delimiter = None
 |
 |  doublequote = None
 |
 |  escapechar = None
 |
 |  lineterminator = None
 |
 |  quotechar = None
 |
 |  quoting = None
 |
 |  skipinitialspace = None

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.