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

后端 未结 6 1527
清酒与你
清酒与你 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:19

    Using the unicode.translate method:

    >>> table = dict(zip(map(ord,u'0123456789'),map(ord,u'0123456789')))
    >>> print u'123'.translate(table)
    123
    

    It requires a mapping of code points as numbers, not characters. Also, using u'unicode literals' leaves the values unencoded.

提交回复
热议问题