def get_script(self): ret = UEMPTY try: ret = decode(self.buf, self.encoding) except UnicodeDecodeError: encodings = ['GB18030', 'SHIFT-JIS', 'UTF-8'] if os.path.isfile(self.path): encodings.extend(['UTF-16LE', 'BIG5']) if self.encoding in encodings: encodings.remove(self.encoding) for encoding in encodings: try: ret = decode(self.buf, encoding) self.encoding = encoding break except UnicodeDecodeError: pass if ret: return ret raise ONSHandlerError('Can not decode script')
def read_char_dict(data_path, encoding=None): chardict = {} try: buf = open(data_path, 'rb').read() buf = decode(buf, encoding) for data in buf.splitlines(): k, v = data.strip().split(' ', 1) chardict[k] = v except Exception as e: print(e) else: print('Success load char dict: %s, %s' % (data_path, len(chardict))) return chardict