Linux 如何将 ISO8859-15 转换为 UTF8?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11316986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:13:53  来源:igfitidea点击:

How to convert ISO8859-15 to UTF8?

linuxbashtext

提问by Hakim

I have an Arabic file encoded in ISO8859-15. How can I convert it into UTF8?
I used iconvbut it doesn't work for me.

我有一个用ISO8859-15编码的阿拉伯语文件。如何将其转换为UTF8?
我用过,iconv但对我不起作用。

iconv -f ISO-8859-15 -t UTF-8 Myfile.txt

I wanted to attach the file, but I don't know how.

我想附上文件,但我不知道如何附上。

采纳答案by HighKing

Could it be that your file is not ISO-8859-15 encoded? You should be able to check with the file command:

可能是您的文件不是 ISO-8859-15 编码的吗?您应该可以使用 file 命令进行检查:

file YourFile.txt

Also, you can use iconv without providing the encoding of the original file:

此外,您可以使用 iconv 而不提供原始文件的编码:

iconv -t UTF-8 YourFile.txt

回答by Colin Keenan

I found this to work for me:

我发现这对我有用:

iconv -f ISO-8859-14 Agreement.txt -t UTF-8 -o agreement.txt

回答by Li.Gui

in my case, the filecommand tells a wrong encoding, so i tried converting with all the possible encodings, and found out the right one.

就我而言,该file命令给出了错误的编码,因此我尝试使用所有可能的编码进行转换,并找到了正确的编码。

execute this script and check the result file.

执行此脚本并检查结果文件。

for i in `iconv -l`
do
   echo $i
   iconv -f $i -t UTF-8 yourfile | grep "hint to tell converted success or not"
done &>/tmp/converted

回答by Nuri Akman

You can use ISO-8859-9 encoding:

您可以使用 ISO-8859-9 编码:

iconv -f ISO-8859-9 Agreement.txt -t UTF-8 -o agreement.txt

回答by aburbanol

I have ubuntu 14 and the other answers where no working for me

我有 ubuntu 14 和其他对我不起作用的答案

iconv -f ISO-8859-1 -t UTF-8 in.tex > out.tex

I found this command here

我在这里找到了这个命令

回答by Sanchez-1987

Iconv just writes the converted text to stdout. You have to use -o OUTPUTFILE.txtas an parameter or write stdout to a file. (iconv -f x -t z filename.txt > OUTPUTFILE.txtoriconv -f x -t z < filename.txt > OUTPUTFILE.txtin some iconv versions)

Iconv 只是将转换后的文本写入标准输出。您必须-o OUTPUTFILE.txt用作参数或将标准输出写入文件。(iconv -f x -t z filename.txt > OUTPUTFILE.txticonv -f x -t z < filename.txt > OUTPUTFILE.txt在某些 iconv 版本中)

Synopsis

iconv -f encoding -t encoding inputfile

Description

The iconv program converts the encoding of characters in inputfile from one coded character set to another. 
**The result is written to standard output unless otherwise specified by the --output option.**

--from-code, -f encoding

Convert characters from encoding

--to-code, -t encoding

Convert characters to encoding

--list

List known coded character sets

--output, -o file

Specify output file (instead of stdout)

--verbose

Print progress information.

回答by nanoguo

I got the same problem, but i find the answer in this page! it works for me, you can try it.

我遇到了同样的问题,但我在这个页面找到了答案 !它对我有用,你可以试试。

iconv -f cp936 -t utf-8?

回答by Charles Santos

We have this problem and to solve

我们有这个问题并要解决

Create a script file called to-utf8.sh

创建一个名为 to-utf8.sh 的脚本文件

#!/bin/bash
TO="UTF-8"; FILE=
FROM=$(file -i $FILE | cut -d'=' -f2)
if [[ $FROM = "binary" ]]; then
 echo "Skipping binary $FILE..."
 exit 0
fi
iconv -f $FROM -t $TO -o $FILE.tmp $FILE; ERROR=$?
if [[ $ERROR -eq 0 ]]; then
  echo "Converting $FILE..."
  mv -f $FILE.tmp $FILE
else
  echo "Error on $FILE"
fi

Set the executable bit

设置可执行位

chmod +x to-utf8.sh

Do a conversion

进行转换

./to-utf8.sh MyFile.txt

If you want to convert all files under a folder, do

如果要转换文件夹下的所有文件,请执行

find /your/folder/here | xargs -n 1 ./to-utf8.sh

Hope it's help.

希望它有帮助。