[python] 문자열에서 기호 제거
참조 자료:
################
파이썬에서 string의 translate 메소드를 이용한다.
문자열에서 기호 제거를 위해서는 다음과 같이 하면 된다.
import string
s = "string. With. Punctuation?" # Sample string
out = s.translate(string.maketrans("",""), string.punctuation)
################
string의 translate 메소드는 리눅스의 tr 명령과 유사한 역할을 한다.
s.translate(table, deletechars)
translate는 string의 replace와 유사한 역할을 할 수 있는데, replace보다 translate가 더 빠르다. (참조: Best way to strip punctuation from a string in Python)
################
파이썬에서 string의 translate 메소드를 이용한다.
문자열에서 기호 제거를 위해서는 다음과 같이 하면 된다.
import string
s = "string. With. Punctuation?" # Sample string
out = s.translate(string.maketrans("",""), string.punctuation)
################
string의 translate 메소드는 리눅스의 tr 명령과 유사한 역할을 한다.
s.translate(table, deletechars)
- table: 변환할 문자 목록
- table = string.maketrans ("abc", "123") 와 같은 형태로 사용 (이 경우, a는 1로, b는 2로, c는 3으로 변경한다)
- deletechars: 삭제할 문자들
translate 함수 사용예는 Python String translate() Method 를 참조하면 된다.
string.punctuation 는 모든 아스키(ascii) 문자를 나타낸다.
################
translate는 string의 replace와 유사한 역할을 할 수 있는데, replace보다 translate가 더 빠르다. (참조: Best way to strip punctuation from a string in Python)
댓글