def utf8_to_code(text, encoding): encoding = string.upper(encoding) if encoding == 'UTF-8': return text #Note: Pass through to wstrop. This means we don't play nice and #Escape characters that are not in the target encoding. ws = wstring.from_utf8(text) text = ws.encode(encoding) #This version would skip all untranslatable chars: see wstrop.c #text = ws.encode(encoding, 1) return text
def utf8_to_code(text, encoding): encoding = string.upper(encoding) if encoding == 'UTF-8': return text from xml.unicode.iso8859 import wstring wstring.install_alias('ISO-8859-1', 'ISO_8859-1:1987') #Note: Pass through to wstrop. This means we don't play nice and #Escape characters that are not in the target encoding. ws = wstring.from_utf8(text) text = ws.encode(encoding) #This version would skip all untranslatable chars: see wstrop.c #text = ws.encode(encoding, 1) return text