删除序数超出范围的字符串中的所有字符 [英] Remove all characters from a string who's ordinals are out of range

查看:117
本文介绍了删除序数超出范围的字符串中的所有字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从python中的字符串中删除所有不在范围内的字符的好方法是什么?

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

我在python 2.7中使用hashlib.sha256.我遇到了例外情况:

I'm using hashlib.sha256 in python 2.7. I'm getting the exception:

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

UnicodeEncodeError: 'ascii' codec can't encode character u'\u200e' in position 13: ordinal not in range(128)

我认为这意味着某些时髦的字符进入了我要哈希的字符串中.

I assume this means that some funky character found its way into the string that I am trying to hash.

谢谢!

推荐答案

new_safe_str = some_string.encode('ascii','ignore') 

我认为会起作用

或者您可以进行列表理解

or you could do a list comprehension

"".join([ch for ch in orig_string if ord(ch)<= 128])

[edit]但是正如其他人所说的,通常最好弄清楚如何处理unicode ...除非出于某种原因您真的需要将其编码为ascii

[edit] however as others have said it may be better to figure out how to deal with unicode in general... unless you really need it encoded as ascii for some reason

这篇关于删除序数超出范围的字符串中的所有字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆