CSV 檔案開頭的 U+FEFF 位元組順序標記 (BOM) 是一個不可見的 Unicode 字元,可能會導致指令碼、資料庫匯入和試算表工具發生錯誤。這個字元通常會被文字編輯器或匯出程序自動加入,雖然它代表 UTF-8 編碼,但當檔案被未預期處理它的工具讀取時,可能會造成解析錯誤。移除 BOM 對於確保與 Excel、MySQL、PostgreSQL 等系統以及 Python、JavaScript 或 PHP 等程式語言的相容性至關重要。BOM 移除工具透過精確地去除一個前置的 U+FEFF,同時保留檔案中其他所有字元(包括刻意使用的逗號、引號、換行符號,甚至後續出現的 U+FEFF)來解決這個問題。這能確保您的 CSV 資料保持完整且可用,無需手動編輯或使用複雜的命令列工具。
CSV 檔案被廣泛用於資料交換,因為它們簡單、人類可讀,且幾乎所有資料處理工具都支援。然而,它們的簡單性可能會被像 BOM 這樣的隱藏字元所破壞。例如,將 CSV 匯入資料庫時,BOM 可能會導致第一個欄位名稱被誤讀,進而造成匯入失敗或資料對應錯誤。同樣地,處理 CSV 檔案的指令碼若在檔案開頭遇到未預期的 BOM,可能會發生錯誤。這些問題令人困擾,因為 BOM 在大多數文字編輯器中是不可見的,很難診斷問題的根本原因。BOM 移除工具提供了快速且可靠的方式來移除 BOM,而不會更改檔案的其他內容,進而解決了這個問題。
與手動方法(例如在文字編輯器中開啟檔案並刪除第一個字元)不同,BOM 移除工具既精確又可重複。它確保只會移除前置的 U+FEFF,檔案其餘部分則保持不動。這對於在文字後段刻意包含 U+FEFF 字元的 CSV 檔案特別重要,例如在帶引號的欄位或多語言資料中。該工具也能在您的瀏覽器中本地執行,代表不需要將資料上傳到伺服器,過程既快速又安全。無論您正在處理個人專案的小型 CSV 檔案,或是專業應用程式的大型資料集,BOM 移除工具都能為這個常見問題提供一致的解決方案。

為什麼 BOM 會破壞 CSV 檔案
位元組順序標記 (BOM) 是一個 Unicode 字元 (U+FEFF),用於表示文字檔案的編碼方式。在 UTF-8 中,BOM 是選擇性的且通常是不必要的,但某些工具(例如 Microsoft Excel 或特定的文字編輯器)在儲存檔案時會自動加入它。雖然 BOM 有助於識別 UTF-8 編碼,但當檔案被未預期處理它的工具讀取時,也可能會造成問題。例如:
- 資料庫匯入:像 phpMyAdmin、MySQL Workbench 或 PostgreSQL 的 COPY 指令這類工具,若檔案中含有 BOM,可能會無法識別第一個欄位名稱,進而導致「unknown column」或「invalid header」之類的錯誤。
- 指令碼處理:Python、JavaScript 或 PHP 等程式語言若遇到含 BOM 的 CSV 檔案,可能會擲出語法錯誤或誤讀第一行。例如,使用
csv.reader的 Python 指令碼可能會將 BOM 視為第一個欄位的一部分,導致資料損毀。 - 試算表工具:雖然 Excel 在某些情況下能處理 BOM,但其他像 Google Sheets 或 LibreOffice Calc 等工具可能會將 BOM 顯示為不可見字元,或無法正確解析檔案。
- 版本控制:BOM 可能會在 Git 等版本控制系統中造成不必要的差異,使追蹤檔案的實際變更變得更困難。
這些問題特別麻煩,因為 BOM 在大多數文字編輯器中是不可見的,導致難以診斷。即使 BOM 可見,手動移除也可能容易出錯,特別是當檔案在文字後段刻意包含 U+FEFF 字元時。BOM 移除工具透過僅針對前置的 BOM,同時保留檔案其餘內容,消除了這種猜測。
如何從 CSV 檔案中移除 BOM
使用 BOM 移除工具從 CSV 檔案中移除 BOM 是一個直接的過程,只需幾個步驟。該工具完全在您的瀏覽器中執行,因此不會上傳或分享任何資料。以下是操作步驟:
- 解碼 CSV 檔案:在支援 UTF-8 編碼的文字編輯器中開啟 CSV 檔案(例如 Notepad++、VS Code 或 Sublime Text)。確保檔案以解碼格式儲存或檢視,因為 BOM 移除工具需要純文字輸入。如果檔案是以 UTF-8 with BOM 編碼,編輯器會顯示不含可見 BOM 的文字,但 BOM 仍存在於底層資料中。
- 複製文字:選取 CSV 檔案中的所有文字(包括不可見的 BOM)並複製到剪貼簿。請小心不要意外省略第一個字元,因為 BOM 就位於該處。
- 貼上到 BOM 移除工具:在瀏覽器中開啟 BOM 移除工具。將複製的文字貼到輸入欄位中。該工具會自動偵測是否存在前置的 U+FEFF。
- 執行移除作業:點擊「Remove BOM」按鈕。工具會處理文字並顯示狀態訊息,告知是否找到並移除了 BOM。如果未偵測到 BOM,狀態會確認文字未變更。
- 檢視輸出結果:工具會在輸出欄位中顯示清理後的文字。請確認 CSV 檔案的第一行(例如標題列)顯示正確,且檔案其餘部分未發生非預期的變更。該工具會保留所有換行符號、逗號、引號和其他字元,包括文字後段任何刻意使用的 U+FEFF。
- 複製清理後的文字:選取輸出文字並複製到剪貼簿。您現在可以將其貼回文字編輯器,並視需求儲存為新檔案或覆寫原始檔案。
這個過程能確保在不移除檔案其他內容的情況下移除 BOM。對於包含複雜資料的 CSV 檔案(例如含有逗號的帶引號欄位或多語言文字),該工具特別有用,因為它會保留所有刻意使用的字元,僅針對前置的 BOM。
需要移除 BOM 的常見情境
BOM 會在多種情境中造成問題,從資料庫匯入到指令碼處理皆然。以下是一些需要從 CSV 檔案中移除 BOM 的常見情況:
| 情境 | BOM 造成的問題 | 解決方案 |
|---|---|---|
| 將 CSV 匯入資料庫 | BOM 會導致第一個欄位名稱被誤讀,進而造成「unknown column」或「invalid header」之類的錯誤。 | 使用 BOM 移除工具在將檔案匯入 MySQL、PostgreSQL 或 phpMyAdmin 等工具之前,先去除前置的 U+FEFF。 |
| 在 Python 中處理 CSV 檔案 | csv.reader 模組可能會將 BOM 視為第一個欄位的一部分,導致資料損毀或語法錯誤。 |
使用該工具移除 BOM,然後使用 csv.reader 或 pandas.read_csv 讀取檔案。 |
| 將 CSV 上傳到雲端服務 | Google Sheets、AWS S3 或 Salesforce 等服務若檔案中含有 BOM,可能無法正確解析。 | 在上傳前使用 BOM 移除工具清理檔案,以確保相容性。 |
| 使用 Git 進行版本控制 | BOM 可能會在 Git 中造成不必要的差異,使追蹤檔案的實際變更變得更困難。 | 在提交檔案前移除 BOM,以避免虛假的差異。 |
| 從 CSV 產生報表 | Power BI、Tableau 或自訂報表指令碼等工具若檔案中含有 BOM,可能會誤讀 CSV 檔案的第一行。 | 使用 BOM 移除工具確保檔案在產生報表前是乾淨的。 |
在上述每個情境中,BOM 移除工具都能提供快速且可靠的方式來移除前置的 U+FEFF,而不會更改檔案的其他內容。這能確保您的 CSV 資料保持完整,並與您使用的工具相容。
如何預防 CSV 檔案產生 BOM
雖然 BOM 移除工具是處理現有檔案的絕佳解決方案,但從源頭預防 BOM 的產生可以節省時間並避免相容性問題。以下是避免 CSV 檔案產生 BOM 的一些最佳實務:
- 使用支援 BOM 控制的文字編輯器:Notepad++、VS Code 或 Sublime Text 等編輯器允許您在不含 BOM 的情況下儲存檔案。例如在 Notepad++ 中,您可以在儲存檔案前選擇「編碼」>「轉換成 UTF-8 (無 BOM)」。在 VS Code 中,您可以在設定中設定
"files.encoding": "utf8"以預設避免 BOM。 - 匯出不含 BOM 的 CSV 檔案:從 Excel、Google Sheets 或資料庫等工具匯出 CSV 檔案時,請檢查匯出設定以確保不會加入 BOM。在 Excel 中,您可以使用「另存新檔」選項,並選擇「CSV UTF-8 (逗號分隔) (*.csv)」且不含 BOM。在 Google Sheets 中,使用「下載」>「逗號分隔值 (.csv)」選項,該選項不會包含 BOM。
- 使用命令列工具:如果您是以程式方式產生 CSV 檔案,可以使用
iconv或sed等工具在匯出過程中去除 BOM。例如,您可以使用以下指令從檔案中移除 BOM:sed -i '1s/^\xEF\xBB\xBF//' file.csv。 - 在使用前驗證檔案:在產生或匯出 CSV 檔案後,請在文字編輯器中開啟並檢查是否有 BOM。如果存在,請在使用該檔案於指令碼或匯入前,使用 BOM 移除工具將其移除。
- 記錄您的工作流程:如果您經常處理 CSV 檔案,請記錄您為避免 BOM 所採取的步驟。這可以包括以特定編碼儲存檔案、使用特定工具或在使用前驗證檔案。將這些文件與您的團隊分享,有助於在協作專案中預防 BOM 相關的問題。
遵循這些最佳實務可以降低在 CSV 檔案中遇到 BOM 相關問題的風險。然而,如果您確實遇到 BOM,BOM 移除工具提供了一種快速可靠的方式來移除它,而不會更改檔案的其他內容。
Removing BOMs in Excel and Other Spreadsheet Tools
Excel and other spreadsheet tools can sometimes add or retain BOMs in CSV files, especially when saving or exporting data. Here’s how to handle BOMs in these tools:
- Excel:
- When saving a file as CSV in Excel, choose "CSV UTF-8 (Comma delimited) (*.csv)" from the "Save as type" dropdown. This option typically does not include a BOM, but it’s always a good idea to verify the file in a text editor after saving.
- If you open a CSV file in Excel and notice that the first column name appears incorrect (e.g., with an invisible character), the file likely contains a BOM. Use the BOM Remover tool to clean the file before reopening it in Excel.
- For large datasets, consider using Power Query to import CSV files. Power Query can handle BOMs more gracefully than the standard Excel import process.
- Google Sheets:
- Google Sheets does not add BOMs when exporting CSV files, but it can retain BOMs if the file was imported with one. To avoid issues, clean the file with the BOM Remover tool before importing it into Google Sheets.
- If you’re using Google Apps Script to process CSV files, ensure your script accounts for potential BOMs by stripping them before parsing the data.
- LibreOffice Calc:
- LibreOffice Calc can handle BOMs in some cases, but it’s best to remove them before importing the file. Use the BOM Remover tool to clean the file, then open it in LibreOffice Calc.
- When saving a file as CSV in LibreOffice Calc, select "Edit filter settings" and ensure the "UTF-8" option is selected without BOM.
By understanding how spreadsheet tools handle BOMs, you can take steps to avoid or remove them before they cause issues. The BOM Remover tool is a valuable resource for cleaning files that have already been affected by BOMs, ensuring compatibility with any tool or script you use.
Advanced: Handling BOMs in Scripts and Code
If you’re working with CSV files in scripts or code, you can handle BOMs programmatically to avoid issues. Here are some examples of how to remove BOMs in different programming languages:
- Python:
import codecs def remove_bom(file_path): with open(file_path, 'r', encoding='utf-8-sig') as f: content = f.read() with open(file_path, 'w', encoding='utf-8') as f: f.write(content) remove_bom('file.csv')The
utf-8-sigencoding in Python automatically strips BOMs when reading a file. You can then write the file back without the BOM usingutf-8encoding. - JavaScript (Node.js):
const fs = require('fs'); function removeBom(filePath) { const content = fs.readFileSync(filePath, 'utf8'); const cleanedContent = content.replace(/^\uFEFF/, ''); fs.writeFileSync(filePath, cleanedContent, 'utf8'); } removeBom('file.csv');This script reads the file, removes the leading U+FEFF, and writes the cleaned content back to the file.
- PHP:
function removeBom($filePath) { $content = file_get_contents($filePath); if (substr($content, 0, 3) === "\xEF\xBB\xBF") { $content = substr($content, 3); } file_put_contents($filePath, $content); } removeBom('file.csv');This PHP function checks for the BOM signature and removes it if present.
While these scripts can handle BOMs programmatically, the BOM Remover tool provides a simpler, browser-based solution for users who don’t want to write or run code. It’s particularly useful for non-technical users or for quickly cleaning files without setting up a development environment.
Related guide: How to Remove a Leading BOM From Text in Your Browser.
Related reading: Calculate Chmod Values Quickly Using Octal and Symbolic Notation.
Related reading: How to Generate Code Image From Text Locally.