> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> CSV 格式的文档

# CSV

<div id="description">
  ## 描述
</div>

逗号分隔值格式 ([RFC](https://tools.ietf.org/html/rfc4180)) 。
进行格式化时，行会用双引号括起来。字符串内的双引号会输出为两个连续的双引号。
没有其他字符转义规则。

* Date 和 date-time 会用双引号括起来。
* 数字输出时不加引号。
* 值之间由分隔符字符分隔，默认为 `,`。分隔符字符由设置 [format\_csv\_delimiter](/docs/zh/reference/settings/formats#format_csv_delimiter) 定义。
* 行之间使用 Unix 换行符 (LF) 分隔。
* Array 在 CSV 中按如下方式序列化：
  * 首先，数组会像在 TabSeparated 格式中一样被序列化为字符串
  * 然后，将生成的字符串用双引号括起来输出到 CSV。
* Tuple 在 CSV 格式中会被序列化为单独的列 (也就是说，会丢失其在 Tuple 中的嵌套结构) 。

```bash theme={null}
$ clickhouse-client --format_csv_delimiter="|" --query="INSERT INTO test.csv FORMAT CSV" < data.csv
```

<Note>
  默认情况下，分隔符为 `,`
  更多信息请参见设置 [format\_csv\_delimiter](/docs/zh/reference/settings/formats#format_csv_delimiter)。
</Note>

解析时，所有值都可以带引号或不带引号。支持双引号和单引号。

行也可以不带引号。在这种情况下，会一直解析到分隔符或换行符 (CR 或 LF) 为止。
不过，这不符合 RFC：解析不带引号的行时，会忽略行首和行尾的空格与制表符。
支持以下换行类型：Unix (LF)、Windows (CR LF) 和 Mac OS Classic (CR LF)。

`NULL` 按照设置 [format\_csv\_null\_representation](/docs/zh/reference/settings/formats#format_csv_null_representation) 进行格式化 (默认值为 `\N`) 。

在输入数据中，`ENUM` 值可以表示为名称或 id。
首先，我们会尝试将输入值与 `ENUM` 名称匹配。
如果失败，且输入值是数值，则会尝试将该数值与 `ENUM` id 匹配。
如果输入数据仅包含 `ENUM` id，建议启用设置 [input\_format\_csv\_enum\_as\_number](/docs/zh/reference/settings/formats#input_format_csv_enum_as_number) 以优化 `ENUM` 解析。

<div id="example-usage">
  ## 使用示例
</div>

<div id="format-settings">
  ## 格式设置
</div>

| 设置                                                                                                                                                    | 描述                                                                                   | 默认值     | 说明                                                                                                                                         |
| ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------ | ------- | ------------------------------------------------------------------------------------------------------------------------------------------ |
| [format\_csv\_delimiter](/docs/zh/reference/settings/formats#format_csv_delimiter)                                                                         | CSV 数据中用作分隔符的字符。                                                                     | `,`     |                                                                                                                                            |
| [format\_csv\_allow\_single\_quotes](/docs/zh/reference/settings/formats#format_csv_allow_single_quotes)                                                   | 允许使用单引号括起的字符串。                                                                       | `true`  |                                                                                                                                            |
| [format\_csv\_allow\_double\_quotes](/docs/zh/reference/settings/formats#format_csv_allow_double_quotes)                                                   | 允许使用双引号括起的字符串。                                                                       | `true`  |                                                                                                                                            |
| [format\_csv\_null\_representation](/docs/zh/reference/settings/formats#format_tsv_null_representation)                                                    | CSV 格式中的自定义 NULL 表示形式。                                                               | `\N`    |                                                                                                                                            |
| [input\_format\_csv\_empty\_as\_default](/docs/zh/reference/settings/formats#input_format_csv_empty_as_default)                                            | 将 CSV 输入中的空字段视为默认值。                                                                  | `true`  | 对于复杂的默认表达式，还必须启用 [input\_format\_defaults\_for\_omitted\_fields](/docs/zh/reference/settings/formats#input_format_defaults_for_omitted_fields)。 |
| [input\_format\_csv\_missing\_nullable\_as\_empty\_string](/docs/zh/reference/settings/formats#input_format_csv_missing_nullable_as_empty_string)          | 将缺失的 `Nullable(String)` 值读取为空字符串而非 NULL，而不受 `input_format_csv_empty_as_default` 的影响。 | `false` |                                                                                                                                            |
| [input\_format\_csv\_enum\_as\_number](/docs/zh/reference/settings/formats#input_format_csv_enum_as_number)                                                | 将插入到 CSV 格式中的枚举值视为枚举索引。                                                              | `false` |                                                                                                                                            |
| [input\_format\_csv\_use\_best\_effort\_in\_schema\_inference](/docs/zh/reference/settings/formats#input_format_csv_use_best_effort_in_schema_inference)   | 在 CSV 格式中使用一些优化和启发式方法来推断 schema。如果禁用，则所有字段都会被推断为 String。                             | `true`  |                                                                                                                                            |
| [input\_format\_csv\_arrays\_as\_nested\_csv](/docs/zh/reference/settings/formats#input_format_csv_arrays_as_nested_csv)                                   | 从 CSV 读取 Array 时，会假定其元素先以嵌套 CSV 形式序列化，再放入字符串中。                                       | `false` |                                                                                                                                            |
| [output\_format\_csv\_crlf\_end\_of\_line](/docs/zh/reference/settings/formats#output_format_csv_crlf_end_of_line)                                         | 如果设置为 true，CSV 输出格式的行尾将使用 `\r\n` 而不是 `\n`。                                           | `false` |                                                                                                                                            |
| [input\_format\_csv\_skip\_first\_lines](/docs/zh/reference/settings/formats#input_format_csv_skip_first_lines)                                            | 跳过数据开头指定数量的行。                                                                        | `0`     |                                                                                                                                            |
| [input\_format\_csv\_detect\_header](/docs/zh/reference/settings/formats#input_format_csv_detect_header)                                                   | 在 CSV 格式中自动检测包含名称和 types 的表头。                                                        | `true`  |                                                                                                                                            |
| [input\_format\_csv\_skip\_trailing\_empty\_lines](/docs/zh/reference/settings/formats#input_format_csv_skip_trailing_empty_lines)                         | 跳过数据末尾的空行。                                                                           | `false` |                                                                                                                                            |
| [input\_format\_csv\_trim\_whitespaces](/docs/zh/reference/settings/formats#input_format_csv_trim_whitespaces)                                             | 去除未加引号的 CSV 字符串中的空格和制表符。                                                             | `true`  |                                                                                                                                            |
| [input\_format\_csv\_allow\_whitespace\_or\_tab\_as\_delimiter](/docs/zh/reference/settings/formats#input_format_csv_allow_whitespace_or_tab_as_delimiter) | 允许在 CSV 字符串中使用空白字符或制表符作为字段分隔符。                                                       | `false` |                                                                                                                                            |
| [input\_format\_csv\_allow\_variable\_number\_of\_columns](/docs/zh/reference/settings/formats#input_format_csv_allow_variable_number_of_columns)          | 允许 CSV 格式中的列数可变，忽略多余列，并为缺失列使用默认值。                                                    | `false` |                                                                                                                                            |
| [input\_format\_csv\_use\_default\_on\_bad\_values](/docs/zh/reference/settings/formats#input_format_csv_use_default_on_bad_values)                        | 当 CSV 字段因值不合法而反序列化失败时，允许为列设置默认值。                                                     | `false` |                                                                                                                                            |
| [input\_format\_csv\_try\_infer\_numbers\_from\_strings](/docs/zh/reference/settings/formats#input_format_csv_try_infer_numbers_from_strings)              | 在 schema inference 期间尝试从字符串字段中推断数值。                                                  | `false` |                                                                                                                                            |
