前些天,一个客户提供了一个CSV文件,说导入MySQL失败,文件在2G左右,后来测试知道是Unicode(UTF-16LE)格式的缘故,用UltraEdit打开另存为UTF8格式,再次导入成功。不过对于这样大的文件,Ultraedit打开就颇为吃力了,如果文件再大的话就更困难了,于是想到可以使用另外一个利器来做字符集编码的转换,iconv。
网上有很多windows的编译版本,我这里提供的是利用mxe编译的静态win32版本,
iconv是一个计算机程序以及一套应用程序编程接口的名称。它的作用是在多种国际编码格式之间进行文本内码的转换。支持的内码包括:
各国采用的ANSI编码,其中包括GB2312、BIG5等中文编码方式。
作为应用程序的iconv采用命令行界面,允许将某种特定编码的文件转换为另一种编码。
用法,
Usage: iconv.exe [OPTION...] [-f ENCODING] [-t ENCODING] [INPUTFILE...]
or: iconv.exe -l
Converts text from one encoding to another encoding.
Options controlling the input and output format:
-f ENCODING, --from-code=ENCODING
the encoding of the input
-t ENCODING, --to-code=ENCODING
the encoding of the output
Options controlling conversion problems:
-c discard unconvertible characters
--unicode-subst=FORMATSTRING
substitution for unconvertible Unicode characters
--byte-subst=FORMATSTRING substitution for unconvertible bytes
--widechar-subst=FORMATSTRING
substitution for unconvertible wide characters
Options controlling error output:
-s, --silent suppress error messages about conversion problems
Informative output:
-l, --list list the supported encodings
--help display this help and exit
--version output version information and exit
Report bugs to .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Usage:iconv.exe[OPTION...][-fENCODING][-tENCODING][INPUTFILE...]
or:iconv.exe-l
Convertstextfromoneencodingtoanotherencoding.
Optionscontrollingtheinputandoutputformat:
-fENCODING,--from-code=ENCODING
theencodingoftheinput
-tENCODING,--to-code=ENCODING
theencodingoftheoutput
Optionscontrollingconversionproblems:
-cdiscardunconvertiblecharacters
--unicode-subst=FORMATSTRING
substitutionforunconvertibleUnicodecharacters
--byte-subst=FORMATSTRINGsubstitutionforunconvertiblebytes
--widechar-subst=FORMATSTRING
substitutionforunconvertiblewidecharacters
Optionscontrollingerroroutput:
-s,--silentsuppresserrormessagesaboutconversionproblems
Informativeoutput:
-l,--listlistthesupportedencodings
--helpdisplaythishelpandexit
--versionoutputversioninformationandexit
Reportbugsto.
例子,
文件infile从UTF-16LE编码转换至UTF-8编码并写入到文件outfile中:
iconv -f UTF-16LE -t utf-8 infile > outfile
1
iconv-fUTF-16LE-tutf-8infile>outfile