macOS 使用 enca 识别 文件编码类型(比 file 命令准确)
文章目录
- macOS 上安装 enca
- 基本使用
- 起因 - iconv
- 关于 enca
- 安装 Enca
- enca & enconv 其它用法
macOS 上安装 enca
brew install enca
基本使用
enca filepath.txt
示例
$ enca 动态规划算法.txt [0]
Simplified Chinese National Standard; GB2312
CRLF line terminators
起因 - iconv
在macOS 上打开一些 .txt
文件,会显示乱码(非 utf-8 编码),我想使用 iconv 命令对文件进行转码。
此时使用 file
命令来获得文件编码,在使用 iconv 转码时会出错。使用 enca 获取的则正确。
副 iconv 转码命令:
iconv -f GB2312 -t UTF-8 分治算法.txt > 分治算法2.txt
iconv 使用可详见:https://ezcode.blog.csdn.net/article/details/146444439
关于 enca
如果解决问题之余,还想更多了解 enca 可以往下看。
enca github : https://github.com/nijel/enca
Enca : Extremely Naive Charset Analyser,极简的字符集分析器
由两个主要组件组成:
- libenca,一个编码检测库。
它目前支持白俄罗斯语、保加利亚语、克罗地亚语、捷克语、爱沙尼亚语、匈牙利语、拉脱维亚语、立陶宛语、波兰语、俄语、斯洛伐克语、斯洛文尼亚语、乌克兰语、中文以及一些独立于语言的多字节编码。
API 应该相对稳定(可以理解为“它要么只发生微小的变化,要么发生非常剧烈的变化”)。 - enca,一个命令行前端,集成了 libenca 和几个字符集转换库和工具(GNU recode、UNIX98 iconv、perl Unicode::Map、cstocs)。
安装 Enca
Enca 应该可以在每个符合 POSIX.1 规范的系统上使用 ISO C 编译器编译并运行,实际上它也可以在许多不符合规范的系统上编译(下面列出依赖项)。
如果您拥有以下附加工具,Enca 可以使用它们作为外部转换器:
- GNU recode 和相关的编码库
- Perl 字符集转换器 Unicode::Map8 或 Unicode::Map
- cstocs,著名的捷克字符集转换器
可选功能:
- GNU recode 库接口的编译由
--with-librecode[=DIR]
,--without-librecode
配置参数控制。
默认情况下,如果找到它则会编译进去。您还可以指定一个文件夹;librecode 包含文件将在此DIR/include
中搜索,库本身在DIR/lib
中。 - UNIX98 iconv 接口的编译由
--with-libiconv=[DIR]
,--without-libiconv
配置参数控制。
默认情况下,如果找到并且被认为可用则会编译进去。您还可以指定一个 DIR;libiconv 包含文件将在此 DIR/include 中搜索,库本身在 DIR/lib 中。 - 外部转换程序接口的编译由
--enable-external
,--disable-external
配置参数控制。默认情况下是编译进去的。
不要尝试在以下不支持 ISO C 和 POSIX 功能的系统上编译 Enca:
- 函数原型。
- 基本的 ISO C 头文件及其声明的函数:
- assert.h, ctype.h, math.h, stdarg.h, stdio.h, stdlib.h
- 任意(工作状态)的 string.h, strings.h, memory.h
- unistd.h, sys/stat.h, sys/types.h
对于有耐心的人:像往常一样运行以下命令
./configure
make
make check
make install
enca & enconv 其它用法
输入 enca --help
可以查看可用选项
也可以输入 man enca
获得更多说明
$ enca --help [0]
Usage: enca [-L LANGUAGE] [OPTION]... [FILE]...
enconv [-L LANGUAGE] [OPTION]... [FILE]...
Detect encoding of text files and convert them if required.
Operation modes:
-g, --guess Behave as `enca' (just detect encoding by default)
-c, --auto-convert Behave as `enconv' (autoconvert by default)
Output type selectors:
-d, --details Print failure reason when encoding was not recognized
-e, --enca-name Print enca's encoding name (passed to converters)
-f, --human-readable Print full (descriptive) encoding name (default)
-i, --iconv-name Print how iconv calls the encoding
-m, --mime-name Print preferred MIME encoding name
-r, --rfc1345-name Print RFC 1345 (or otherwise canonized) encoding name
-s, --cstocs-name Print how cstocs calls the encoding
-n, --name=WORD Print required name (enca-name, human-readable, etc.)
-x, --convert-to=ENC Convert file to some other encoding ENC
Guessing parameters:
-L, --language=LANG Set language of FILEs; obligatory, when cannot be
determined from locale settings
Conversion parameters:
-E, --external-converter-program=PATH
Set external converter program name
(default: piconv)
-C, --try-converters=LIST Converters to be tried (associative)
(default: built-in,iconv)
General options:
-p, --with-filename Print the file name for each result
-P, --no-filename Suppress the prefixing filename on output
-V, --verbose Increase verbosity level
Listings:
-G, --license Print full enca license and terminate
-h, --help Print this help and terminate
-l, --list=WORD Print required list (built-in-charsets, converters,
charsets, languages, lists, names, surfaces)
and terminate
-v, --version Print version and build information and terminate
With no FILE, reads standard input and possibly writes converted stream to
standard output. Exit status is 0 if all files were successfully proceeded,
1 if some were not recognized or converted, 2 in real troubles.
When called as `enconv' without -x, target encoding it guessed from locales.
Report bugs to https://github.com/nijel/enca/issues
2025-03-22(六)