一、万能打印这些小技巧在平常的项目中可能会常常遇到,将这些小技巧记录下来,以便以后再学习。
qqq = '12345678912121212121' print(type(qqq)) print('%r'% qqq)二、10进制转16进制
hex(16) -----> 0x10三、16进制转10进制
int(‘0x10’, 16) ==> 16 示例实现方法 addr = int('0x'+str(int(value)), 16)四、正则表达式
eeee = r'd+,d+,d+' result = re.match(eeee, '0,0,0')
匹配逻辑表达式:(1,1,1)(-1,1.7,1) def isMyStr(s): p = re.compile('^(((-?d+)(.d+)?),){2}((-?d+)(.d+)?)$') if p.match(s): return True else: return False五、json、dict、str转化 5.1 json.dumps()函数
将字典转化为字符串 dict = { "xxx": "1", "yyy": "2", "zzz": "3" } json_str = json.dumps(dict, ensure_ascii=False) print(json_str)5.2 json.loads()函数
json_str = '{"xxx": "1","yyy": "2","zzz": "3"}' dict = json.loads(json_str) print(type(dict)) print(dict)5.3 json.dump()函数
将json信息写进文件
参考5.1示例字典
path = "文件路径" if os.path.exists(path) == False: with open(path, 'w+', encoding='utf-8') as f: json.dump(dict, f)5.4 json.load()函数
将读取文件的json信息
path = "文件路径" if os.path.exists(path) == False: with open(path, 'r', encoding='utf-8') as f: dict_1 = json.load(f)六、获取当前目录的文件夹名称
pathName = os.path.basename(path)七、byte str互转
str 转 byte byte = string.encode('utf-8') byte 转 str string = byte.decode('utf-8') aaa = '123' b = aaa.encode('utf-8') print(b) bbb = b.decode('utf-8') print(bbb)八、加解密
key = Fernet.generate_key() key = b'AG1ePgaWwlJ3TDyAj5yZOMh20WRoGiya349VQkjI-Gg=' cipher = Fernet(key) text = b'5555' #进行加密 encrypted_text = cipher.encrypt(text) print(encrypted_text) #进行解密 decrypted_text = cipher.decrypt(encrypted_text) print(decrypted_text)九、获取主板序列号(PC唯一属性之一)
def get_mainboard_info(): """ 主板序列号 :return:主板号 list list[0] 即为主板号 """ s = wmi.WMI() mainboard = [] for board_id in s.Win32_baseBoard(): mainboard.append(board_id.SerialNumber.strip().strip('.')) return mainboard
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)