从序列超出范围的字符串中删除所有字符

Chr*_*row 3 python regex ascii ordinal hashlib

删除范围之外的所有字符的好方法是什么:ordinal(128)来自python中的字符串?

我在python 2.7中使用hashlib.sha256.我得到了例外:

UnicodeEncodeError:'ascii'编解码器无法对位置13中的字符u'\ u200e'进行编码:序数不在范围内(128)

我认为这意味着一些时髦的角色进入了我想要散列的字符串.

谢谢!

Jor*_*ley 5

new_safe_str = some_string.encode('ascii','ignore') 
Run Code Online (Sandbox Code Playgroud)

我认为会奏效

或者你可以做一个列表理解

"".join([ch for ch in orig_string if ord(ch)<= 128])
Run Code Online (Sandbox Code Playgroud)

[编辑]然而,正如其他人所说,最好弄清楚如何处理unicode一般...除非你真的需要它编码为ascii由于某种原因