复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46#!/usr/bin/python # _*_ coding: utf-8 _*_ import os import io import json import re import jsonpath import hashlib def md5_passwd(str, key='123456'): #satl是盐值,默认是123456 str=str+key md = hashlib.md5() # 构造一个md5对象 md.update(str.encode()) res = md.hexdigest() return res def gbk_to_utf8(result): result = json.dumps(result) s_to_gbk = result.encode("gbk") #gbk需要先解码成unicode,再编码成utf-8 gbk_to_utf8 = s_to_gbk.decode("gbk").encode("utf-8") #解码成unicode字符编码 utf8_decode = gbk_to_utf8.decode("utf-8") utf8_decode = json.loads(utf8_decode) return utf8_decode def utf8_to_gbk(result): result = json.dumps(result) s_to_utf = result.encode("utf-8") #gbk需要先解码成unicode,再编码成utf-8 utf_to_gbk = s_to_utf.decode("utf-8").encode("gbk") #解码成unicode字符编码 gbk_decode = utf_to_gbk.decode("gbk") gbk_decode = json.loads(gbk_decode) return gbk_decode def bianma(res): #改变标准输出的默认编码 sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') result = res.decode('gb18030') return result if __name__ == '__main__': # print(get_today(-1)) cwxx = b'xc3xdcxc2xebxb2xbbxd5xfdxc8xb7!' cwxx = 'u6570u636eu5305u65e0u6548uff1au5e10u6237u65e0u6548' cwxx = b'\u07b4\u02ff\u037b' # print(cwxx) print(bianma(cwxx))
最后
以上就是精明汽车最近收集整理的关于Python编码格式互相转换的全部内容,更多相关Python编码格式互相转换内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复