Python으로 파일에서 특정 단어 찾기와 치환하기텍스트 파일을 다루다 보면 특정 단어를 찾아서 바꾸거나, 몇 번 등장했는지 세거나, 위치까지 확인해야 할 때가 있습니다. 이 글에서는 Python의 re 모듈과 문자열 메서드를 활용해 그 방법을 정리합니다.1. 특정 단어 치환하기import rereplace_dict = { "dog": "cat", "hello": "hi"}with open("input.txt", "r", encoding="utf-8") as f: content = f.read()for old, new in replace_dict.items(): pattern = r"\b" + re.escape(old) + r"\b" content = re.sub(pattern..