如何使用 fast-csv 将特殊字符写入节点中的 csv 文件
How do I write special characters to csv file in node with fast-csv
我有一个包含德语字符的列表,我使用 fast-csv 写入 csv 文件并给出了不同的字符。
import { writeToPath } from "fast-csv";
const data = [
{
"name": "Ästhetik Zahnarzt Hamburg City Z-24 Mö 17 / Dr. P. Michaelis, Dr. C. Khorram",
"street_number": "Speersort 8",
"zip_location": "20095 Hamburg, City Innenstadt",
"phone": "040 34 45 44",
"website": "http://www.z-24.de",
"area": "Branche: Zahnärzte"
}
]
writeToPath("tmp.csv", data, { headers: true })
.on("error", (err) => console.error(err))
.on("finish", () => console.log("Done writing."));
输出是这样的:
screenshot of output data
Ästhetik Zahnarzt Hamburg City Z-24 Mö 17 / Dr. P. Michaelis, Dr. C. Khorram | ... | Branche: Zahnärzte
这是猜测 CSV 编码的 Excel 问题。
只需指明选项writeBOM
等于true
,这样Excel就可以猜出编码是UTF-8。
最可靠的方法是直接写入 XLSX 文件以避免此类编码问题。
我有一个包含德语字符的列表,我使用 fast-csv 写入 csv 文件并给出了不同的字符。
import { writeToPath } from "fast-csv";
const data = [
{
"name": "Ästhetik Zahnarzt Hamburg City Z-24 Mö 17 / Dr. P. Michaelis, Dr. C. Khorram",
"street_number": "Speersort 8",
"zip_location": "20095 Hamburg, City Innenstadt",
"phone": "040 34 45 44",
"website": "http://www.z-24.de",
"area": "Branche: Zahnärzte"
}
]
writeToPath("tmp.csv", data, { headers: true })
.on("error", (err) => console.error(err))
.on("finish", () => console.log("Done writing."));
输出是这样的: screenshot of output data
Ästhetik Zahnarzt Hamburg City Z-24 Mö 17 / Dr. P. Michaelis, Dr. C. Khorram | ... | Branche: Zahnärzte
这是猜测 CSV 编码的 Excel 问题。
只需指明选项writeBOM
等于true
,这样Excel就可以猜出编码是UTF-8。
最可靠的方法是直接写入 XLSX 文件以避免此类编码问题。