Back

linux - 使用file , iconv 转换文件编码 convert file encoding using iconv, gbk, gb2312 utf-8 转换文件编码 csv 乱码

发布时间: 2021-01-16 09:08:00

查看某个文件的encoding:  file -i <some_file> 

index.htm.old: text/html; charset=iso-8859-1  (注意,这里出现的情况不一定是真的... 实际上该文件是 gb2312 )

查看 iconv 支持的 编码类型:   iconv -l 

结果有好多, 例如 UTF-8,  GB-2312, ISO-8859-1

转换: iconv index.htm.old -f GB-2312 -t UTF-8

转换到文件中: 绝对不要使用 > , 要使用 -o. 例如

$ iconv index.htm.old -f GB-2312 -t UTF-8 -o index.htm

如果运行上面的命令时,提示说 GB-2312 not supported: iconv: conversion from `GB-2312' is not supported , 那就重新配置 dpkg-reconfigure --locale 

$ locale -a 

看上面的结果中是否包含了 GB-2312的编码方式,应该是没有包含。就需要重新配置。

$ sudo dpkg-reconfigure --force locales  ,

en_ZM.UTF-8... done
en_ZW.UTF-8... done
zh_CN.GB2312... done
zh_CN.GBK... done
zh_CN.UTF-8... done
zh_SG.UTF-8... done

然后选中  GB2312   , 保存。 

然后再次运行 $ iconv index.htm.old -f GB2312 -t UTF-8 -o index.htm  

(记得上面的 GB2312, 在有的ubuntu版本中是 GB2312, 有的是  GB-2312, 不一样的。具体情况根据locale -a 来分析)
就可以了

Back