나는 RE에 익숙하지 않지만 목록을 반복하고 re.sub
을 사용하여 변수에 들어있는 큰 텍스트 블록에서 여러 항목을 추출하려고합니다. first_word
. 찾을 단어 목록을 가진 re.sub
re.sub
를 사용하여이 잘 작동하지만 나는 옆에
exclusionList
변수에 모든 문자열을 제거 할 내가이 작업을 수행하는 방법을 모르겠습니다.
도움을 주셔서 감사합니다. 예외를 제기하는 코드는 다음과 같습니다.
exclusionList = ['+','of','<ET>f.','to','the','<L>L.</L>']
for a in range(0, len(exclusionList)):
first_word = re.sub(exclusionList[a], '',first_word)
그리고 예외 :
first_word = re.sub(exclusionList[a], '',first_word)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 245, in _compile
raise error, v # invalid expression error: nothing to repeat
어떤 예외가 발생합니까? – Junuxx
first_word = re.sub (exclusionList [a], '', first_word) 파일 /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py "줄 151, 하위 return _compile (pattern, flags) .sub (repl, string, count) 파일 "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", 줄 245, _compile에 raise 오류, v # 표현이 잘못되었습니다 오류 : 반복 할 항목이 없습니다 –
Junuxx, 도움 감사합니다. 나는 단순히 '\ +'를 exclusionList에 추가했고 이제는 작동합니다. 목록이 정적이기 때문에 이것은 나에게 훌륭한 해결책이다. 도와 주셔서 감사합니다. –