Remove all characters from a string who's ordinals are out of range

前端 未结 3 1562
眼角桃花
眼角桃花 2021-01-14 07:34

What is a good way to remove all characters that are out of the range: ordinal(128) from a string in python?

I\'m using hashlib.sha256 in python 2.7. I\

3条回答
  •  滥情空心
    2021-01-14 08:17

    Instead of removing those characters, it would be better to use an encoding that hashlib won't choke on, utf-8 for example:

    >>> data = u'\u200e'
    >>> hashlib.sha256(data.encode('utf-8')).hexdigest()
    'e76d0bc0e98b2ad56c38eebda51da277a591043c9bc3f5c5e42cd167abc7393e'
    

提交回复
热议问题