티스토리 뷰

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

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

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함