Python:如何用半角字符替换全角字符?

栏目: Python · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/2422177/python-how-can-i-replace-full-width-characters-with-half-width-characters
如果这是 PHP

,我可能会这样做:

function no_more_half_widths($string){
  $foo = array('1','2','3','4','5','6','7','8','9','10')
  $bar = array('1','2','3','4','5','6','7','8','9','10')
  return str_replace($foo, $bar, $string)
}

我在 python 中尝试过.translate函数,它表明数组的大小不同.我认为这是因为个别字符以utf-8编码.有什么建议?

内置的unicodedata模块可以做到:

>>> import unicodedata
>>> foo = u'1234567890'
>>> unicodedata.normalize('NFKC', foo)
u'1234567890'

“NFKC”代表“ Normalization Form KC [兼容性分解,然后是规范组合]”,并用半宽的字符替换全宽字符,即 Unicode equivalent .

请注意,它还可以同时对各种其他内容进行标准化,例如单独的重音符号和罗马数字符号.

翻译自:https://stackoverflow.com/questions/2422177/python-how-can-i-replace-full-width-characters-with-half-width-characters


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Big Java Late Objects

Big Java Late Objects

Horstmann, Cay S. / 2012-2 / 896.00元

The introductory programming course is difficult. Many students fail to succeed or have trouble in the course because they don't understand the material and do not practice programming sufficiently. ......一起来看看 《Big Java Late Objects》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具