Python:如何用半角字符替换全角字符? [英] 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')返回 str_replace($foo, $bar, $string)}
我在 python 中尝试了 .translate 函数,它表明数组的大小不同.我认为这是因为单个字符以 utf-8 编码.有什么建议?
内置的unicodedata
模块可以做到:
NFKC"代表规范化表格 KC [兼容性分解,然后是规范组合]",并将全角字符替换为半角字符,即 Unicode 等效.>
请注意,它还同时规范了各种其他内容,例如单独的重音符号和罗马数字符号.
If this was PHP, I would probably do something like this:
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)
}
I have tried the .translate function in python and it indicates that the arrays are not of the same size. I assume this is due to the fact that the individual characters are encoded in utf-8. Any suggestions?
The built-in unicodedata
module can do it:
>>> import unicodedata
>>> foo = u'1234567890'
>>> unicodedata.normalize('NFKC', foo)
u'1234567890'
The "NFKC" stands for "Normalization Form KC [Compatibility Decomposition, followed by Canonical Composition]", and replaces full-width characters by half-width ones, which are Unicode equivalent.
Note that it also normalizes all sorts of other things at the same time, like separate accent marks and Roman numeral symbols.
这篇关于Python:如何用半角字符替换全角字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!