excel 打开csv文件乱码

背景

从Linux导出1个csv文件到Windows,excel和记事本打开中文都显示乱码;
Linux执行file 1.csv 显示文件编码是UTF-8

处理过程

查询得知Windows是用BOM来标记文本文件得编码方式;
用记事本打开文件-->文件另存为-->保存1个编码为“带有BOM的UTF-8”的文件-->直接用excel打开新文件

优化

导出csv文件到Windows时,注意检查文件是否有BOM标记

# Linux中查询当前UTF-8文件的BOM标记
:set bomb?

# 加BOM标记
:set bomb

# 删除BOM标记
:set nobomb

# 或者处理文件的时候Add BOM to the new file
#!/usr/bin/env bash

#Add BOM to the new file
printf '\xEF\xBB\xBF' > with_bom.txt

# Append the content of the source file to the new file
cat source_file.txt >> with_bom.txt
posted @ 2022-10-24 00:02  gamcau_fung  阅读(69)  评论(0编辑  收藏  举报