Open Chinese Convert (OpenCC) 是一个开源的中文简繁转换项目。
安装Python的 opencc-python-reimplemented
库:
pip install opencc-python-reimplemented
字符串用代码,文件用命令完事。
下面直接简单抄一抄官方的描述
from opencc import OpenCC
cc = OpenCC('s2t') # convert from Simplified Chinese to Traditional Chinese
# can also set conversion by calling set_conversion
# cc.set_conversion('s2tw')
to_convert = '开放中文转换'
converted = cc.convert(to_convert)
usage: python -m opencc [-h] [-i <file>] [-o <file>] [-c <conversion>]
[--in-enc <encoding>] [--out-enc <encoding>]
optional arguments:
-h, --help show this help message and exit
-i <file>, --input <file>
Read original text from <file>. (default: None = STDIN)
-o <file>, --output <file>
Write converted text to <file>. (default: None = STDOUT)
-c <conversion>, --config <conversion>
Conversion (default: None)
--in-enc <encoding> Encoding for input (default: UTF-8)
--out-enc <encoding> Encoding for output (default: UTF-8)
example with UTF-8 encoded file:
python -m opencc -c s2t -i my_simplified_input_file.txt -o my_traditional_output_file.txt
See https://docs.python.org/3/library/codecs.html
#standard-encodings for list of encodings.
hk2s
: 繁中(香港) → 简中s2hk
: 简中 → 繁中(香港)s2t
: 简中 → 繁中s2tw
: 简中 → 繁中(台湾)s2twp
: 简中 → 繁中(台湾,带短语)t2hk
: 繁中 → 繁中(香港)t2s
: 繁中 → 简中t2tw
: 繁中 → 繁中(台湾)tw2s
: 繁中(台湾) → 简中tw2sp
: 繁中(台湾) → 简中(带短语)当转换有两个以上的字词可能时,程序只会使用第一个。