Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly are the csv module's Dialect settings for excel-tab?

Tags:

python

csv

excel

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel.

What if I want to know??

All kidding aside, I want to know specifically which attributes and settings would create the dialect csv.excel_tab

Dialect.delimiter A one-character string used to separate fields.

Dialect.doublequote Controls how instances of quotechar appearing inside a field should themselves be quoted.

Dialect.escapechar A one-character string used by the writer to escape the delimiter if quoting is set to QUOTE_NONE and the quotechar if doublequote is False.

Dialect.lineterminator The string used to terminate lines produced by the writer. It defaults to '\r\n'.

Dialect.quotechar A one-character string used to quote fields containing special characters, such as the delimiter or quotechar, or which contain new-line characters.

Dialect.quoting Controls when quotes should be generated by the writer and recognised by the reader. It can take on any of the QUOTE_* constants (see section Module Contents) and defaults to QUOTE_MINIMAL.

Dialect.skipinitialspace When True, whitespace immediately following the delimiter is ignored. The default is False.

Dialect.strict When True, raise exception Error on bad CSV input. The default is False.

like image 788
xtian Avatar asked Mar 10 '18 02:03

xtian


People also ask

What is CSV format in Excel?

A CSV is a comma-separated values file, which allows data to be saved in a tabular format. CSVs look like a garden-variety spreadsheet but with a . csv extension. CSV files can be used with most any spreadsheet program, such as Microsoft Excel or Google Spreadsheets.

What is .CSV file format example?

A CSV file is a list of data separated by commas. For instance, it may look like the following: Name,email,phone number,address. Example,[email protected],555-555-5555,Example Address.

What is the default delimiter in CSV file?

When you save a workbook as a .csv file, the default list separator (delimiter) is a comma. You can change this to another separator character using Windows Region settings.

What does CSV DictWriter do?

The csv. DictWriter class operates like a regular writer but maps Python dictionaries into CSV rows. The fieldnames parameter is a sequence of keys that identify the order in which values in the dictionary passed to the writerow method are written to the CSV file.


1 Answers

Going straight to the source of Lib/csv.py, excel-tab has all the properties of excel, plus a tab-delimiter.

class excel(Dialect):
    """Describe the usual properties of Excel-generated CSV files."""
    delimiter = ','
    quotechar = '"'
    doublequote = True
    skipinitialspace = False
    lineterminator = '\r\n'
    quoting = QUOTE_MINIMAL
register_dialect("excel", excel)

class excel_tab(excel):
    """Describe the usual properties of Excel-generated TAB-delimited files."""
    delimiter = '\t'
register_dialect("excel-tab", excel_tab)
like image 158
Brad Solomon Avatar answered Nov 03 '22 01:11

Brad Solomon