Python可以使用OpenCC库来进行繁简体转换。首先,需要使用pip安装OpenCC库,命令如下:
pip install opencc-python-reimplemented
然后,可以使用以下代码来将简体中文转换为繁体中文:
import opencc
converter = opencc.OpenCC('s2t.json') # 's2t.json'表示简体中文转繁体中文
text = '这是简体中文'
converted_text = converter.convert(text)
print(converted_text) # 输出:這是繁體中文
类似地,可以将繁体中文转换为简体中文,代码如下:
import opencc
converter = opencc.OpenCC('t2s.json') # 't2s.json'表示繁体中文转简体中文
text = '這是繁體中文'
converted_text = converter.convert(text)
print(converted_text) # 输出:这是简体中文