1. 程式人生 > >Linux下打開windows中文文本亂碼問題

Linux下打開windows中文文本亂碼問題

nic col one inf 文檔 instr ins pen ram

1. 查看文件的編碼方式:file命令

$ file test_file.txt
test_file.txt: ISO-8859 text, with very long lines

$ file train_model.py 
train_model.py: Python script, UTF-8 Unicode text executable

$ file 接口文檔.docx 
接口文檔.docx: Microsoft Word 2007+

但是file命令不太可靠, 一個gb2312編碼的文件被判斷為ISO-8859

2. 在gedit用指定的編碼方式打開文件

打開gedit, 然後File => Open => 左下角的Character Encoding的右邊有個下拉列表,選擇Add or Remove... => 出現下圖所示的設置界面,從左邊列表中選擇需要的編碼方式添加到右邊列表

技術分享圖片

然後在打開文件的時候選定需要的編碼方式即可:

技術分享圖片

3. 轉碼: iconv命令

$ iconv -f gb2312 -t utf-8 test_file.txt -o test_file_utf8.txt
$ file test_file_utf8.txt
test_file_utf8.txt: UTF-8 Unicode text

iconv命令的用法具體如下:

$ iconv --help
Usage: iconv [OPTION...] [FILE...]
Convert encoding of given files from one encoding to another.

 Input/Output format specification:
  -f, --from-code=NAME       encoding of original text
  -t, --to-code=NAME         encoding for output

 Information:
  
-l, --list list all known coded character sets Output control: -c omit invalid characters from output -o, --output=FILE output file -s, --silent suppress warnings --verbose print progress information -?, --help Give this help list --usage Give a short usage message -V, --version Print program version Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options. For bug reporting instructions, please see: <https://bugs.launchpad.net/ubuntu/+source/glibc/+bugs>.

參考:https://www.cnblogs.com/longwaytogo/p/6308703.html

Linux下打開windows中文文本亂碼問題