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