Python: How can I replace full-width characters with half-width characters?

后端 未结 6 1555
清酒与你
清酒与你 2020-12-16 00:51

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         


        
6条回答
  •  难免孤独
    2020-12-16 01:29

    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.

提交回复
热议问题