2012-10-25 91 views

回答

4

使用 '替换abcdükl×M' 的正则表达式[^a-zA-Z]

re.sub(r'[^a-zA-Z]', '', mystring) 

一些信息:a-zA-Z是分别表示所有小写字母和大写字母的字符范围,字符类别开头的脱字符号表示否定,例如“除这些之外的任何东西”

1

搜索[^a-zA-Z],代之以'

2

假设您尝试对文本进行规范化,请参阅“Comprehensive character replacement module in python for non-unicode and non-ascii for HTML”下的链接。

unicodedatanormalize方法,可以适度降低文本您:

import unicodedata 
def gracefully_degrade_to_ascii(text): 
    return unicodedata.normalize('NFKD',text).encode('ascii','ignore') 

全部文档 - http://docs.python.org/library/unicodedata.html

如果你想只是去掉非ASCII字符,则否定的字符集其他人提到的正则表达式就是这样做的。

0
>>> import string 
>>> print ''.join(x if x in string.ascii_letters else ' ' for x in u'abcdükl*m') 
abcd kl m 
相关问题