2016-09-15 2 views
0

내가 이렇게 보이는 데이터 세트를을 사용하여 삭제는, 그래서 나는 모든 @와 연결된 단어를 제거하려합니다. 내 데이터 세트는 이와 같이 보일 것입니다.하위 문자열 찾기 및 정규식, 파이썬

"See the new #Gucci 5th Ave NY windows customized by for the debut of the #GucciGhost collection." 
    "Before the #GucciGhost collection debuts tomorrow, read about the artist" 

그래서 간단한 대체 문을 사용하여 @을 제거 할 수 있습니다. 그러나 인접 단어는 문제입니다.

나는 re를 사용하여 발생을 검색/찾습니다. 그러나 나는이 단어를 지울 수 없다.

피씨 - 한 단어 인 경우 문제가되지 않았을 것입니다. 그러나 여러 단어는 당신이 당신이 원하는 당신을 줄 것이다 정규식을

import re 

a = [ 
"See the new #Gucci 5th Ave NY windows customized by @troubleandrew for the debut of the #GucciGhost collection.", 
"Before the #GucciGhost collection debuts tomorrow, read about the artist @troubleandrew" 
] 
pat = re.compile(r"@\S+") # \S+ all non-space characters 
for i in range(len(a)): 
    a[i] = re.sub(pat, "", a[i]) # replace it with empty string 
print a 

을 사용할 수 있습니다 @

+0

무엇이 문제입니까? 어떤 코드가 @ + 단어를 제거하지 못합니까? '리 '해봤 니? –

+0

내 문제는 내가 전체 @ + 단어를 삭제할 수 없다는 것이 었습니다. 나는're.findall'을 사용하고있었습니다. 어쨌든're.sub'가 작동합니다. 감사 –

답변

2

에 부착 내 데이터 세트에 여기 저기에있다.

0

개성있는 버전, 잠수함 여분의 공간 :

import re 

a = [ 
    "See the new #Gucci 5th Ave NY windows customized by @troubleandrew for the debut of the #GucciGhost collection.", 
    "Before the #GucciGhost collection debuts tomorrow, read about the artist @troubleandrew" 
] 

rgx = re.compile(r"\[email protected]\S+") 

b = [ re.sub(rgx, "", row) for row in a ] 

print b 

\s? : \s 일치 ' 'zero or one 발생에 대한 ? 의미합니다.