Python 汉字简体和繁体的相互转换
<div align="left"><p style="margin: 10px auto; color: rgb(57, 57, 57); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; background-color: rgb(250, 247, 239);"><span style="line-height: 1.5;">其实利用python实现汉字的简体和繁体相互转早有人做过,并发布到github上了,地址:</span><a href="https://github.com/skydark/nstools/tree/master/zhtools" target="_blank" style="color: rgb(100, 102, 179); line-height: 1.5;">https://github.com/skydark/nstools/tree/master/zhtools</a></p><p style="margin: 10px auto; color: rgb(57, 57, 57); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; background-color: rgb(250, 247, 239);">该项目还有其他很多跟汉字相关的功能,本文只介绍繁体和简体相互转换</p><p style="margin: 10px auto; color: rgb(57, 57, 57); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; background-color: rgb(250, 247, 239);">具体方法很简单,下载该项目中的 zh_wiki.py 和 langconv.py 两个文件,放到python代码目录下就可以了.</p><p style="margin: 10px auto; color: rgb(57, 57, 57); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; background-color: rgb(250, 247, 239);">我的python是3.5版本,所以在字符串的decode上和python2.x 有所不同,demo:</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important;"><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(100, 102, 179); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 1</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">from</span> langconv <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">import</span> *<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 2</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">import</span><span style="line-height: 1.5 !important;"> sys
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 3</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 4</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">print</span><span style="line-height: 1.5 !important;">(sys.version)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 5</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">print</span><span style="line-height: 1.5 !important;">(sys.version_info)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 6</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 7</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">#</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 转换繁体到简体</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 8</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">def</span><span style="line-height: 1.5 !important;"> cht_to_chs(line):
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;"> 9</span> line = Converter(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">zh-hans</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="line-height: 1.5 !important;">).convert(line)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">10</span> line.encode(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">utf-8</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="line-height: 1.5 !important;">)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">11</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span><span style="line-height: 1.5 !important;"> line
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">12</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">13</span> <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">#</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 转换简体到繁体</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">14</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">def</span><span style="line-height: 1.5 !important;"> chs_to_cht(line):
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">15</span> line = Converter(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">zh-hant</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="line-height: 1.5 !important;">).convert(line)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">16</span> line.encode(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">utf-8</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="line-height: 1.5 !important;">)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">17</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span><span style="line-height: 1.5 !important;"> line
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">18</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">19</span> line_chs=<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;"><>123asdasd把中文字符串进行繁体和简体中文的转换</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">20</span> line_cht=<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;"><>123asdasd把中文字符串進行繁體和簡體中文的轉換</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">21</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">22</span> ret_chs = <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">%s\n</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>%<span style="line-height: 1.5 !important;">cht_to_chs(line_cht)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">23</span> ret_cht = <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">%s\n</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>%<span style="line-height: 1.5 !important;">chs_to_cht(line_chs)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">24</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">25</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">print</span>(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">chs='%s'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="line-height: 1.5 !important;">,ret_cht)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">26</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">print</span>(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">cht='%s'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="line-height: 1.5 !important;">,ret_cht)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">27</span>
<span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">28</span> file = open(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">ret.txt</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span>,<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">w</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span>,encoding=<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">utf-8</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">'</span><span style="line-height: 1.5 !important;">)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">29</span> <span style="line-height: 1.5 !important;">file.write(ret_chs)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">30</span> <span style="line-height: 1.5 !important;">file.write(ret_cht)
</span><span style="color: rgb(0, 128, 128); line-height: 1.5 !important;">31</span> file.close()</pre><div class="cnblogs_code_toolbar" style="margin-top: 5px;"><span class="cnblogs_code_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a title="复制代码" style="color: rgb(100, 102, 179); text-decoration-line: underline; border: none !important;"><img src="https://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="max-width: 900px; height: auto;"></a></span></div></div><p style="margin: 10px auto; color: rgb(57, 57, 57); font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 14px; background-color: rgb(250, 247, 239);">ret.txt文件内容</p><div class="cnblogs_code" style="margin-top: 5px; margin-bottom: 5px; padding: 5px; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); overflow: auto; color: rgb(0, 0, 0); font-family: "Courier New" !important; font-size: 12px !important;"><pre style="white-space: pre-wrap; font-family: "Courier New" !important;"><><span style="line-height: 1.5 !important;">123asdasd把中文字符串进行繁体和简体中文的转换
</span><>123asdasd把中文字符串進行繁體和簡體中文的轉換</pre></div></div>
页:
[1]