# update: 2021-6-30-11 import pickle import json def pickle_dumps(obj): """压缩数据""" return pickle.dumps(obj) def pickle_loads(obj): """解压数据""" return pickle.loads(obj) def json_dumps(obj): """压缩数据""" return json.dumps(obj) def json_loads(obj): """解压数据""" return json.loads(obj) def is_json(string): """是否json""" try: if string: json.loads(string) else: return False except Exception as exception: return False return True def str_to_re(string): """ 转义正则特殊符号 """ string = string.replace('$', r'\$') string = string.replace('(', r'\(') string = string.replace(')', r'\)') string = string.replace('*', r'\*') string = string.replace('.', r'\.') string = string.replace('[', r'\[') string = string.replace(']', r'\]') string = string.replace('?', r'\?') string = string.replace('\\', '\\\\') string = string.replace('^', r'\^') string = string.replace('{', r'\{') string = string.replace('}', r'\}') string = string.replace('|', r'\|') return string def str_to_json(string): """ eval转换 """ null = None true = True false = False return eval(string) def json_to_str(input_data): """ eval转换 """ if type(input_data) == dict: for k, v in input_data.items(): if type(v) != str: continue input_data[k] = v.replace('\'', '') data = str(input_data).replace('\'', '\"') data = str(data).replace('None', 'null') data = str(data).replace('True', 'true') data = str(data).replace('False', 'false') return data if __name__ == '__main__': # --- test --- # a = {111: 222} # print(json_dumps(a)) # print(json_loads(json_dumps(a))) # --- test --- a = b'\x80\x03N.' o = pickle_loads(a) print(o, type(o))