Programming/Python

[Python] 특정 문자(키워드) 주변 앞뒤 단어 출력하기

Oziraper 2020. 10. 27. 14:46

특별한 방법이 있는건 아니지만, 다음과 같은 방법으로 가능합니다.

1. 키워드를 기준으로 split하기

ex) 한번만 split 할 경우
text_list = text.split("키워드", 1)

2. split한 내용 중 index 0의 뒷부분과 index 1의 앞 부분을 자르기

ex) text_list[0]에서 -1~-5, text_list[1]에서 0~4 이런식으로

 

참고

https://stackoverflow.com/questions/38312735/printing-5-words-before-and-after-a-specific-word-in-a-file-in-python

 

printing 5 words before and after a specific word in a file in python

I have a folder which contains some other folders and these folders contain some text files. (The language is Persian). I want to print 5 words before and after a keyword with the keyword in the mi...

stackoverflow.com