2013-02-07 2 views
1

'@', '.'와 같은 모든 특수 문자를 이메일에서 제거하고 싶습니다. 그들을 '밑줄'로 바꿉니다. 파이썬 'unidecode'에 몇 가지 기능이 있지만 제 요구 사항을 모두 채우지는 못합니다. 누구든지 나를 위의 언급 문자를 문자열에서 찾아서 '밑줄'로 대체 할 수 있도록 어떤 식 으로든 제안 할 수 있습니까?장고의 문자열에서 특수 문자를 제거하십시오.

감사합니다.

답변

3

.replace()를 사용하지?

예 :

a='[email protected]' 
a.replace('@','_') 
'testemail_email.com' 

당신은 아마 파이썬 요리 책 2 판에서이

a='[email protected]' 
replace=['@','.'] 
for i in replace: 
    a=a.replace(i,'_') 
+0

을 도와 드리겠습니다.하지만 대체하려면 두 개의 인수 만 필요합니다. txt.replace ('@', '_') 하나 이상의 문자를 제거하려면 어떻게해야합니까? – Inforian

+0

@Inforian 봐 편집 – Jonathan

+0

그래, 고마워 작동합니다 – Inforian

1

는 가이드로 이것을 가지고 :

import re 
a = re.sub(u'[@]', '"', a) 

구문 :

re.sub(pattern, repl, string, max=0) 
+0

안녕 캐시에 대한 모든 신용, 대체

는 – Jonathan

+0

덕분에 서브보다 빠른이며 작동하지만 반대 방향으로 ...이 **[email protected]**로 대체 ""@ "" "" "." ""** – Inforian

+1

[this] (http://stackoverflow.com/questions/5668947/python-string-replace-vs-re-sub) – Jonathan

1

좋은 예처럼 뭔가를 할 수있는 여러 편집 할 수

import string 
def translator(frm='', to='', delete='', keep=None): 
    if len(to) == 1: 
     to = to * len(frm) 
    trans = string.maketrans(frm, to) 
    if keep is not None: 
     allchars = string.maketrans('', '') 
     delete = allchars.translate(allchars, keep.translate(allchars, delete)) 
    def translate(s): 
     return s.translate(trans, delete) 
    return translate 


remove_cruft = translator(frm="@-._", to="~") 
print remove_cruft("[email protected]") 

출력 :

me~and~you~gmail~com 

좋은에게 넣을 문자열 유틸리티 귀하의 툴킷. ""** "the book

관련 문제