У меня есть файл, в котором в качестве разделителя полей используется \x01
, а в качестве ограничителя строки - \x02\n
. Вот пример заголовка файла:
#export_date\x01artist_id\x01name\x01is_actual_artist\x01view_url\x01artist_type_id\x02\n
Когда я использую csv.Sniffer()
, я получаю следующее:
{'module': 'csv', '_name': 'sniffed', 'lineterminator': '\r\n', 'quoting': 0, 'doc': None, 'doublequote': False, 'delimiter': '\x01', 'quotechar': '"', 'skipinitialspace': False}
Что означают следующие три поля:
В приведенном выше тексте нет кавычек, поэтому я бы предположил, что quotechar
должен быть пустым, а не заключать в двойные кавычки, но любое объяснение по этим трем полям было бы замечательно.
Кроме того, я пробовал несколько других файлов и, кажется, всегда дает точный результат для этих трех полей, независимо от того, есть ли в этом файле цитирование или нет.
Проверить документацию
Controls how instances of quotechar appearing inside a field should themselves be quoted. When True, the character is doubled. When False, the escapechar is used as a prefix to the quotechar. It defaults to True.
A one-character string used to quote fields containing special characters, such as the delimiter or quotechar, or which contain new-line characters. It defaults to '"'.
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.
Эти поля описаны в Диалекты и параметры форматирования документы:
- 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.
- doublequote: Controls how instances of quotechar appearing inside a field should themselves be quoted. When True, the character is doubled. When False, the escapechar is used as a prefix to the quotechar. It defaults to True. On output, if doublequote is False and no escapechar is set, Error is raised if a quotechar is found in a field.
- quotechar: A one-character string used to quote fields containing special characters, such as the delimiter or quotechar, or which contain new-line characters. It defaults to '"'.
Касательно:
The above doesn't have any quoting, so I'd assume the quotechar should be empty instead of a double quote, but any explanation on these three fields would be great.
В библиотеке CPython csv.py
есть комментарий, в котором говорится:
# _csv.reader won't accept a quotechar of ''`
Источник: https://github.com/python/cpython/blob/2ef69a1d45de8aa41c45d32d9ee1ff227bb1a566/Lib/csv.py#L198
В вашем случае, если нет цитирования, вы, вероятно, захотите использовать csv.QUOTE_NONE
. Сниффер, вероятно, не смог бы это понять за вас.