2012-10-25 90 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 
相關問題