Python/Python__works

문자열 치환하기

말하는감자 2019. 10. 11. 17:08
test_string = 'asdf!_[]'

# 1 []를 삭제하기 : translate 사용
string01 = test_string.translate({ord('['): '', ord(']'): ''})

# 2 ! 삭제하기 : replace 사용
string02 = test_string.replace('!', '')

# 3 _ 삭제하기 : 여러개의 text를 dict에 넣고 변환해줄때
remove_dict = {
    "_": ""
}
for s in remove_dict.keys():
    if s in test_string:
        string03 = test_string.replace(s, remove_dict[s])

'Python > Python__works' 카테고리의 다른 글

python + shell script  (0) 2019.10.22
lambda  (0) 2019.10.17
python으로 데이터 정리하기  (0) 2019.09.23
python response xml self-end tag issue  (0) 2019.09.20
python으로 xml을 만들어요  (0) 2019.09.20