나는 현재이 자신의 HTML 엔터티 번호 등가물로 확장 된 ASCII 문자를 교체하고 있어요 : 내가하고 싶은 무엇파이썬에서 확장 ASCII를 HTML 엔티티 이름으로 변환하는 방법은 무엇입니까?
s.encode('ascii', 'xmlcharrefreplace')
대신합니다 (HTML 엔티티 이름의 동등한 즉 ©
변환입니다 ©
). 아래의이 작은 프로그램은 내가 실패하려고하는 것을 보여줍니다. 찾기/바꾸기를 제외하고는이 작업을 수행 할 수있는 방법이 있습니까?
#coding=latin-1
def convertEntities(s):
return s.encode('ascii', 'xmlcharrefreplace')
ok = 'ascii: [email protected]#$%^&*()<>'
not_ok = u'extended-ascii: ©®°±¼'
ok_expected = ok
not_ok_expected = u'extended-ascii: ©®°±¼'
ok_2 = convertEntities(ok)
not_ok_2 = convertEntities(not_ok)
if ok_2 == ok_expected:
print 'ascii worked'
else:
print 'ascii failed: "%s"' % ok_2
if not_ok_2 == not_ok_expected:
print 'extended-ascii worked'
else:
print 'extended-ascii failed: "%s"' % not_ok_2
그래서 "찾기/바꾸기"를 제외하고 말했습니다. 즉, 128 문자의 사전을 작성하고 싶지 않습니다. 이 솔루션은 내가 게시 한 코드에서 작동 할 것이다. –
글쎄, 나는 다른 사람들이 언급 한'htmlentitydefs'를 사용하기 위해 코드를 수정했다. 이제는 빌드 할 필요가 없습니다 :) –
더 좋게 보입니다 ... "<"가 '<' –