import iso6937 import codecs codecs.register(lambda encoding_name: iso6937.getregentry() if encoding_name == 'iso6937' else None) def parse_bytes(bytes_as_string): """Parses /x11/xab to bytearray(b'\x11\xab') """ return bytearray([int(b, 16) for b in bytes_as_string.split('/x') if b != '']) def read_charmap(charmap_file): charmap = [] with open(charmap_file) as lines: in_charmap = False for line in lines: if not in_charmap and line == 'CHARMAP\n': in_charmap = True continue if in_charmap and line == 'END CHARMAP\n': in_charmap = False continue if in_charmap: codepoint, char, description = line[:-1].split(maxsplit=2) charmap.append((chr(int(codepoint[2:-1], 16)), parse_bytes(char), description)) return charmap charmap = read_charmap('ISO_6937') for decoded, encoded, description in charmap: if 'NON-SPACING' in description: continue got = encoded.decode(encoding='iso6937') if got != decoded: print("Decoding {encoded} ({description}) should give {decoded}, got {got}".format( encoded=encoded, decoded=decoded, got=got, description=description)) got = bytearray(decoded.encode(encoding='iso6937')) if got != encoded: print("Encoding {decoded} ({description}) should give {encoded}, got {got}".format( encoded=encoded, decoded=decoded, got=got, description=description))