部分處理文件格式代碼

  • 1.轉換編碼:

原編碼\xe7\x97\x85\xe7\x90\x86\xe5\x8f\x8d\xe5\xb0\x84

轉換爲中文編碼:”引出”

import re

def unicodetostr( s ):
    strTobytes = []
    for i in s.split('\\x'):
        if i != '':
            num = int(i,16)
            strTobytes.append(num)
    a = bytes(strTobytes).decode()
    return a
def ti(m):
    s = str(m.group())
    a = unicodetostr(s)
    return a
pat = re.compile(r'(\\x[0-9a-fA-F][0-9a-fA-F])+')
with open('file_path') as file:
    for line in file:
        '''for m in re.finditer(pat,line):
            print(m.group())'''
        print(re.sub(pat,ti,line))
  • 2.讀.pl文件
import joblib
file = 'word.txt'
pkl_load = joblib.load('file_path')
print(pkl_load)
with open(file,'w', encoding='UTF-8') as f:
    f.write(str(pkl_load))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章