Using Dialects when reading and writing CSV in Python

Source:www.opentechguides.com | Date Published: 2019-04-08 11:40:38

When reading and writing CSV files in Python using the csv module, you can specify an optional dialect parameter with the reader and writer function calls. So what is a Dialect?

A dialect is a group of parameters used to define the format of a CSV file. These parameters are:

  1. delimiter: The character used to separate the fields in a CSV file. Default is ,
  2. quotechar: A single character used to quote fields containing special characters. Default is "
  3. doublequote: The value of this parameter can be True or False. When it is True, the quotechar is doubled if it appears in a field. When False, the escapechar is used to escape the quotechar.
  4. escapechar: A single character used to escape the delimiter(when quoting is set to QUOTE_NONE) or quotechar(when doublequote is False)
  5. quoting: The value for this attribute and their meanings are as below.
  6. lineterminator: String used by the writer to indicate the end of a line. Default is '\r\n'
  7. skipinitialspace: When this attribute is set to true, any whitespace just after the delimiter is ignored. Default is False.
  8. strict: When set to True, an exception is raised when input CSV is incorrectly formatted. Default is False.

Functions to manage dialects

Here is a list of functions used to manipulate dialects.


Open Tech Guides | www.opentechguides.com