BOM 是什么What Is a BOM

BOM(Byte Order Mark,字节序标记)是文件开头的一小段不可见字节(UTF-8 下是 EF BB BF)。它的本意是标记"这是 UTF-8"。但 Windows 上的 Excel 在另存为 UTF-8 CSV 时会带上它,而很多程序、数据库、Linux 工具并不期待这个标记,于是首列名前会多出看不见的乱码,或解析直接报错。

A BOM (Byte Order Mark) is an invisible prefix (for UTF-8 it's EF BB BF) meant to flag "this is UTF-8". But Excel on Windows adds it when saving UTF-8 CSV, while many programs, databases and Linux tools don't expect it — so the first column header gets a hidden garbage prefix, or parsing fails outright.

如何识别文件带 BOMHow to Detect a BOM

  • 现象:首列标题在程序里变成"姓名"之类,或数据库报"未知列"。
  • 编辑器:VS Code 右下角编码处会显示"UTF-8 with BOM";Notepad++ 菜单"编码"里可看到"以 UTF-8-BOM 编码"。
  • 命令行:file file.csv 会提示"with BOM";hexdump -C file.csv | head 开头可见 ef bb bf。
  • Symptom: the first header shows like "Name" in programs, or the DB reports an "unknown column".
  • Editor: VS Code shows "UTF-8 with BOM" at bottom-right; Notepad++ shows "Encode in UTF-8-BOM" under Encoding.
  • CLI: file file.csv says "with BOM"; hexdump -C file.csv | head shows ef bb bf at the start.

如何去除 BOMHow to Remove a BOM

方法一:编辑器另存Method 1: Re-save in editor

VS Code:右下角编码 →"以 UTF-8 保存"(不带 BOM);Notepad++:"编码 → 转为 UTF-8 无 BOM 编码"。

VS Code: click the encoding at bottom-right → "Save with Encoding" → UTF-8 (no BOM). Notepad++: Encoding → Convert to UTF-8 without BOM.

方法二:Python 一行Method 2: One line of Python

open('out.csv','w',encoding='utf-8').write(open('in.csv',encoding='utf-8-sig').read())

用 utf-8-sig 读取会自动剥离 BOM,再以普通 utf-8 写回即可。

Reading with utf-8-sig auto-strips the BOM; writing back with plain utf-8 drops it.

提示:不想装环境?用 365Lab 在线 CSV 工具 的编码转换,选择"UTF-8(无 BOM)"即可一键去除,且数据全程本地处理不上传。
Tip: No environment? Use the 365Lab toolkit's encoding converter and pick "UTF-8 (no BOM)" to strip it in one click — all locally, no upload.

最佳实践Best Practice

对外交换 CSV 时,优先用"UTF-8 无 BOM"。如果对接方是 Windows Excel,则可保留 BOM 让其不乱码;但面向程序、API、数据库时务必去掉。不确定时,用 编码修复教程 先确认编码再处理。

For exchanging CSVs, prefer "UTF-8 without BOM". Keep the BOM only when the recipient is Windows Excel; for programs, APIs and databases always strip it. When unsure, confirm encoding first with our garbled-text guide.

想马上试试?打开在线工具,拖入 CSV,选 UTF-8(无 BOM)即可去除标记并预览。

Want to try now? Open the toolkit, drop in your CSV, pick UTF-8 (no BOM) to strip the mark and preview.

打开 CSV 编码转换 →Open Encoding Converter →