파이썬에서 HTML 코드를 처리 할 때 특수 문자 때문에 다음 코드를 사용해야합니다.string.replace 문의 순서를 더 읽기 좋게 만들기
line = string.replace(line, """, "\"")
line = string.replace(line, "'", "'")
line = string.replace(line, "&", "&")
line = string.replace(line, "<", "<")
line = string.replace(line, ">", ">")
line = string.replace(line, "«", "<<")
line = string.replace(line, "»", ">>")
line = string.replace(line, "'", "'")
line = string.replace(line, "“", "\"")
line = string.replace(line, "”", "\"")
line = string.replace(line, "‘", "\'")
line = string.replace(line, "’", "\'")
line = string.replace(line, "■", "")
line = string.replace(line, "•", "-")
내가 대체해야 할 특수 문자가 훨씬 더 많아 보입니다. 이 코드를 더 우아하게 만드는 방법을 알고 있습니까?
는string.replace
이
str
/
unicode
객체의 방법으로 간단하게 사용할 수 있는지 당신이
가능한 중복 [파이썬 문자열에서 HTML 엔티티 디코딩?] (http://stackoverflow.com/questions/2087370/decode-html-entities-in-python-string) –
'string.replace'와'string'에서 가장 유사한 함수들 모듈 le는 더 이상 사용되지 않습니다. http://docs.python.org/library/string.html#deprecated-string-functions –
@Ben James 감사합니다.이 솔루션은 나에게 적합하지만 중복성이 없습니다. 다른 교체 순서 (예. HTML 특수 문자 이외의 다른 것에 따라 1000 개가 넘는 대체물) – xralf