这是一个相对简单的示例,说明如何操作...
# -*- coding: utf-8 -*-
import re
# Test Data
ENCODING_RAW_DATA = (
('latin_1', 'L', u'Hello'), # Latin 1
('iso8859_2', 'E', u'dobrý večer'), # Central Europe
('iso8859_9', 'T', u'İyi akşamlar'), # Turkish
('iso8859_13', 'B', u'Į sveikatą!'), # Baltic
('shift_jis', 'J', u'今日は'), # Japanese
('iso8859_5', 'C', u'Здравствуйте'), # Cyrillic
('iso8859_7', 'G', u'Γειά σου'), # Greek
)
CODE_TO_ENCODING = dict([(chr(ord(code)-64), encoding) for encoding, code, text in ENCODING_RAW_DATA])
EXPECTED_RESULT = u''.join([line[2] for line in ENCODING_RAW_DATA])
ENCODED_DATA = ''.join([chr(ord(code)-64) + text.encode(encoding) for encoding, code, text in ENCODING_RAW_DATA])
FIND_RE = re.compile('[\x00-\x1A][^\x00-\x1A]*')
def decode_single(bytes):
return bytes[1:].decode(CODE_TO_ENCODING[bytes[0]])
result = u''.join([decode_single(bytes) for bytes in FIND_RE.findall(ENCODED_DATA)])
assert result==EXPECTED_RESULT, u"Expected %s, but got %s" % (EXPECTED_RESULT, result)