编解码总结

自己编辑的一些编辑编解码代码

		二进制编解码

编译码:‘xxxxxxx’.encode(encoding=‘utf-8’)
解码:‘xxxxxxxxxxxxx’.decode(encoding=‘utf-8’)

'r’读取文件:
with open(‘xxxxxx.txt’, ‘r’, encoding=‘utf_8’)as file:
content = file.read()
print(content)
'w’写入文件:
file =open(‘xxxxxxxxx.txt’, ‘w’, encoding=‘utf_8’)
file.write(‘xxxxxxxxxxx\n’)
file.write(‘xxxxxxxxxxx\n’)
file.close()

读取图片:with open(‘xxxxxxxxxxx.jpg’, ‘rb’)as file:
print(file.read())
解码图片:img_bytes =b""“xxxxxxxxxxxxxx”""
with open(‘xxxxxxxxxxx.jpg’, ‘wb’)as file:
file.write(img_bytes)

		base64编解码

base64加密:import base64
content = base64.b64encode(b’xxxxxxxxxxxxx’)
print(content)

base64解密:import base64
img = base64.b64decode(‘xxxxxxxxxxxxxxxx’)
print(img)

加密图片:import base64
with open(‘xxxxxxxxxx’, mode=‘rb’)as f:
img_bytes = f.read()
img_base64 = base64.b64encode(img_bytes)
print(img_base64)

解密图片:import base64
img_base64=b’xxxxxxxxxxxxxx’
img_bytes=base64.b64decode(img_base64)
with open(‘xxxxxxxx’,‘wb’)as file:
file.write(img_bytes)

你可能感兴趣的:(python学习)