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

旧巷老猫 提交于 2021-01-27 06:27:08

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/49204639/what-exactly-are-the-csv-modules-dialect-settings-for-excel-tab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!