コード例 #1
0
 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')
コード例 #2
0
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
コード例 #3
0
ファイル: gbk2sjis.py プロジェクト: AliceQin900/nstools
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