跳到工具
ViewCSV
无需上传文件

CSV 转 YAML

orders.csv· 示例数据15 行 · 8 列
值类型
看起来像数字、true/false 或日期的文本值会加引号,读回时仍是原来的文本。
- order_id: "001041"
  customer: 磊 黄
  city: 北京
  product: Coffee beans 1kg
  quantity: "7"
  unit_price: "139.12"
  order_date: "2026-01-27"
  status: pending
- order_id: "001042"
  customer: 杰 徐
  city: 北京
  product: Coffee beans 1kg
  quantity: "2"
  unit_price: "132.10"
  order_date: "2026-02-14"
  status: shipped
- order_id: "001043"
  customer: 伟 孙
  city: 上海
  product: Notebook A5
  quantity: "7"
  unit_price: "129.75"
  order_date: "2026-03-22"
  status: pending
- order_id: "001044"
  customer: 敏 杨
  city: 上海
  product: Chair, ergonomic
  quantity: "1"
  unit_price: "122.25"
  order_date: "2026-04-25"
  status: paid
- order_id: "001045"
  customer: 静 王
  city: 成都
  product: Coffee beans 1kg
  quantity: "9"
  unit_price: null
  order_date: "2026-05-15"
  status: refunded
- order_id: "001046"
  customer: 伟 黄
  city: 北京
  product: Desk lamp
  quantity: "8"
  unit_price: "104.23"
  order_date: "2026-06-06"
  status: refunded
- order_id: "001047"
  customer: " 伟 吴 "
  city: 成都
  product: USB-C cable
  quantity: "5"
  unit_price: "225.65"
  order_date: "2026-07-08"
  status: refunded
- order_id: "001048"
  customer: 杰 李
  city: 南京
  product: Coffee beans 1kg
  quantity: "7"
  unit_price: "74.98"
  order_date: "2026-08-11"
  status: refunded
- order_id: "001049"
  customer: 磊 王
  city: 南京
  product: Monitor 27"
  quantity: "9"
  unit_price: "223.84"
  order_date: "2026-09-22"
  status: pending
- order_id: "001050"
  customer: 艳 吴
  city: 南京
  product: Desk lamp
  quantity: "3"
  unit_price: "91.94"
  order_date: "2026-01-02"
  status: shipped
- order_id: "001051"
  customer: 娜 刘
  city: 深圳
  product: Monitor 27"
  quantity: "2"
  unit_price: "216.98"
  order_date: "2026-02-18"
  status: pending
- order_id: "001052"
  customer: 磊 刘
  city: 上海
  product: Chair, ergonomic
  quantity: "8"
  unit_price: "136.30"
  order_date: "2026-03-22"
  status: pending
- order_id: "001053"
  customer: 静 张
  city: 北京
  product: Desk lamp
  quantity: "4"
  unit_price: "238.82"
  order_date: "2026-04-25"
  status: pending
- order_id: "001054"
  customer: 艳 陈
  city: 南京
  product: USB-C cable
  quantity: "4"
  unit_price: "95.92"
  order_date: "2026-05-21"
  status: paid
- order_id: "001043"
  customer: 伟 孙
  city: 上海
  product: Notebook A5
  quantity: "7"
  unit_price: "129.75"
  order_date: "2026-03-22"
  status: pending

15 行 · 1 个文件

打开或粘贴 CSV 文件,每一行都会变成 YAML 列表中的一项,以表头为键。默认情况下所有值都保持为文本;YAML 解析器可能会误读的值——NO、yes、0150、2024-03-01、null——会被加上引号,读回时与原来的写法完全相同。文件在你的浏览器中转换,不会上传。

如何把 CSV 转换为 YAML

  1. 点击 打开文件,把 .csv、.tsv 或 .txt 文件拖到工具上,或选择 粘贴。如果键看起来不对,请在 导入设置 中检查分隔符和编码。
  2. 选择 值类型,以及 空单元格 的写法。YAML 标签页中显示的就是下载文件的实际内容。
  3. 选择 缩进,然后点击 复制 或 下载 YAML。

结果是一个顶层序列(由 - key: value 形式的项组成),这是测试夹具、种子数据和静态网站数据文件常用的结构。如果需要 JSON,CSV 转 JSON 提供同样的类型选项。

各选项的作用

所有值都保留为文本默认选项。每个值都是 YAML 字符串;只有当不加引号的写法会被读成别的类型时,才加上引号。
识别数字和 true/false只有当某列所有非空值都是没有前导零、有效数字不超过 15 位的普通数字时,该列才会变成 YAML 数字;true/false 列(大小写不限)会变成布尔值。yes/no 和日期仍是带引号的字符串。数字以规范形式写出,所以 1.50 会变成 1.5。
空单元格null(默认)写成 key: null;空字符串 "" 写成 key: "";省略该键 会在该行中去掉这个键。
缩进列表项下各键使用 2 个空格(默认)或 4 个空格 缩进。

容易产生歧义的值如何加引号

输出的写法保证在 YAML 1.2 解析器和 PyYAML 等较旧的 YAML 1.1 解析器中都能读回同样的值;后者会把更多的词当作布尔值和数字。

  • 国家代码和回答代码:NO、yes、off、y 和 on 都会加引号,所以挪威的国家代码不会被读成 false。键也同样处理("on": x)。
  • 前导零:0150 和 08001 会加引号;不加引号时,YAML 1.1 会把它们读成八进制或十进制数字,并丢掉前导零。
  • 文本模式下的日期和数字:2019-04-01 和 12 会加引号,这样就不会被读成时间戳或整数。
  • 像空值的文本:内容为 null 或 ~ 的单元格会加引号,所以它仍是你写的文本,不会与空单元格混淆。
  • 值内的换行会变成 |- 字面块,文本中的 : 或 # 会加引号。长行永远不会被折叠。
  • 表头完全按原样用作键,包括空格和中文等非拉丁文字。空白表头会变成 Column 3,重复表头变成 name (2)。

常见问题排查

  • 开启“识别数字和 true/false”后,数字列仍然带引号。 至少有一个值不是普通数字,例如 1,5、12 kg 或 007。可以在 CSV 在线查看 中找到它。
  • yes/no 列没有变成布尔值。 只有 true 和 false 会被转换,因为 yes、no 和 on 只在 YAML 1.1 中表示布尔值。如果需要布尔值,请先在 CSV 中替换它们,例如使用 CSV 编辑器。
  • 键中出现了 é 之类的乱码。 文件不是 UTF-8 编码。请在导入设置中选择它的编码(中文 Windows 版 Excel 保存的文件通常选 GB18030)。
  • 所有内容都在一个键下面。 分隔符识别错了;请在导入设置中设置正确的分隔符。

限制与隐私

25 MB、10 万行或 200 万个单元格以内的文件可实时预览前 200 行。更大的文件以流式方式转换:文件分块读取两遍(第一遍测量各列及其类型,第二遍写出),提示会说明下载内容包含整个文件,并且不提供 复制。单个输出文件最大 4 GB。

转换在当前标签页的后台线程中进行,所以页面保持流畅,点击 取消 即可停止。CSV 文件不会发送到任何服务器。

示例

一份客户列表,包含国家代码、带前导零的邮政编码、yes/no 标记、日期和空单元格,使用默认设置转换(全部为文本,空单元格写成 null)。如果开启 识别数字和 true/false,只有 orders 会变化,变成 12、3 和 7。

CSV
country,code,postcode,vat_registered,since,orders
Norway,NO,0150,yes,2019-04-01,12
Spain,ES,08001,no,2021-11-15,3
United States,US,02134,,,7
YAML
- country: Norway
  code: "NO"
  postcode: "0150"
  vat_registered: "yes"
  since: "2019-04-01"
  orders: "12"
- country: Spain
  code: ES
  postcode: "08001"
  vat_registered: "no"
  since: "2021-11-15"
  orders: "3"
- country: United States
  code: US
  postcode: "02134"
  vat_registered: null
  since: null
  orders: "7"

常见问题

为什么 `NO` 带了引号?

在 YAML 1.1 中,不带引号的 NO、no、off 和 n 都表示 false。加上引号后,挪威的国家代码,以及任何写成 no 的回答,在所有解析器中都会保持为文本。

能输出以 ID 为键的单个映射,而不是列表吗?

不能。每行 CSV 都按文件顺序写成一个列表项。这里不提供以键组织的输出。

输出是合法的 YAML 1.2 吗?

是的。为 YAML 1.1 额外添加的引号在 YAML 1.2 中同样合法,它只是把本来就是字符串的值明确标示出来。

标准与参考资料