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

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

内容简介:翻译自: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


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

查看所有标签

猜你喜欢:

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

Mastering JavaServer Faces

Mastering JavaServer Faces

Bill Dudney、Jonathan Lehr、Bill Willis、LeRoy Mattingly / Wiley / 2004-6-7 / USD 40.00

Harness the power of JavaServer Faces to create your own server-side user interfaces for the Web This innovative book arms you with the tools to utilize JavaServer Faces (JSF), a new standard that wi......一起来看看 《Mastering JavaServer Faces》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换