macOS 使用 iconv 转化文件编码

文章目录


使用方式

shell 复制代码
iconv  -f GB2312 -t UTF-8  分治算法.txt > 分治算法2.txt 

支持的编码类型


可以使用 下面命令 查看编码类型

shell 复制代码
iconv -l

PS : ISO-8859 有很多种分支,iconv 支持 ISO-8859-1、ISO-8859-10,但不支持 ISO-8859,否则可能报如下错误:

shell 复制代码
$ iconv  -f ISO-8859   -t UTF-8  分治算法.txt  [0]
iconv: iconv_open(UTF-8, ISO-8859): Invalid argument

这样就不会报错

shell 复制代码
iconv  -f ISO-8859-1   -t UTF-8  分治算法.txt

但具体文件编码类型,建议使用 enca 来查看,file 命令可能不准确

enca 教程:https://blog.csdn.net/lovechris00/article/details/146443005


iconv 更多用法

shell 复制代码
$ iconv --help                                                                                                                  
iconv: unrecognized option `--help'
Usage:	iconv [-cs] -f <from_code> -t <to_code> [file ...]
	iconv -f <from_code> [-cs] [-t <to_code>] [file ...]
	iconv -t <to_code> [-cs] [-f <from_code>] [file ...]
	iconv -l

shell 复制代码
ICONV(1)                                                                                   General Commands Manual                                                                                  ICONV(1)

NAME
     iconv -- codeset conversion utility

SYNOPSIS
     iconv [-cs] -f from_name -t to_name [file ...]
     iconv -f from_name [-cs] [-t to_name] [file ...]
     iconv -t to_name [-cs] [-f from_name] [file ...]
     iconv -l

DESCRIPTION
     The iconv utility converts the codeset of file (or from standard input if no file is specified) from codeset from_name to codeset to_name and outputs the converted text on standard output.

     The following options are available:

     -c    Prevent output of any invalid characters.  By default, iconv outputs an "invalid character" specified by the to_name codeset when it encounts a character which is valid in the from_name codeset
           but does not have a corresponding character in the to_name codeset.

     -f    Specifies the source codeset name as from_name.

     -l    Lists available codeset names.  Note that not all combinations of from_name and to_name are valid.

     -s    Silent.  By default, iconv outputs the number of "invalid characters" to standard error if they exist.  This option prevents this behaviour.

     -t    Specifies the destination codeset name as to_name.

EXIT STATUS
     The iconv utility exits 0 on success, and >0 if an error occurs.

EXAMPLES
     Convert file.txt from IBM273 to UTF-8 and save the result to converted.txt:

           iconv -f IBM273 -t UTF-8 file.txt > converted.txt

SEE ALSO
     mkcsmapper(1), mkesdb(1), iconv(3)

STANDARDS
     iconv conform to IEEE Std 1003.1-2008 ("POSIX.1").

HISTORY
     iconv first appeared in NetBSD 2.0, and made its appearance in FreeBSD 9.0.

macOS 15.1                                                                                    October 22, 2009  

2025-03-22(六)

相关推荐
全栈探索者13 小时前
从素材到成片:ffmpeg 拼接、转码与字幕,一条流水线交付
macos·ffmpeg·aigc·comfyui·视频剪辑
向阳是我16 小时前
Mac 命令行获取 APK 公钥和证书 MD5 指纹
macos
lpfasd12318 小时前
Ollama 还是 LM Studio:在 24GB 的 M6 Mac 上,我把 Ollama 卸载了
macos·策略模式
情 九19 小时前
MCP 开发环境搭建:Mac 与 Windows 跑通 Python 服务和 stdio 客户端
windows·python·macos·mcp
摘星编程19 小时前
Mac玩游戏选什么模拟器?MuMu Mac版体验:金铲铲165帧、明日方舟时停键,操作录制也上线了
macos·玩游戏·远程软件
DARLING Zero two♡19 小时前
Mac远程控制Windows电脑:MacBook远控软件选购指南2026
macos·电脑
fengye2071611 天前
实验:CPython 迭代协议底层验证(`PyObject_GetIter` / `PyIter_Next`)
macos
DevLoom_2 天前
macOS 命令行 curl/git 报 SSL 证书错误?eMudhra 事件完整修复指南
git·macos·ssl
清水白石0082 天前
Python 安装与环境配置:Windows、macOS、Linux 完整指南(第2集)
windows·python·macos
hhhjhl2 天前
开源鸿蒙平台 CMP 三方库「compose-macos-26-ui」适配全流程
macos·开源·harmonyos