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


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

查看所有标签

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

Data-intensive Text Processing With Mapreduce

Data-intensive Text Processing With Mapreduce

Jimmy Lin、Chris Dyer / Morgan and Claypool Publishers / 2010-4-30 / USD 40.00

Our world is being revolutionized by data-driven methods: access to large amounts of data has generated new insights and opened exciting new opportunities in commerce, science, and computing applicati......一起来看看 《Data-intensive Text Processing With Mapreduce》 这本书的介绍吧!

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

在线压缩/解压 HTML 代码

在线进制转换器
在线进制转换器

各进制数互转换器

html转js在线工具
html转js在线工具

html转js在线工具