- 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))