赞
踩
- import base64
-
- source='Hello'
- print(base64.b64encode(source.encode()))
-
- #输出为:b'SGVsbG8='
- import base64
-
- source = '你好'
- print(base64.b64encode(source.encode()))
- print(base64.b64encode(source.encode('GBK')))
- #encode 默认为UTF-8
-
- 输出结果:
-
- b'5L2g5aW9'
- b'xOO6ww=='
- import base64
-
- source = 'xOO6ww=='
- print(base64.b64decode(source))
- print(base64.b64decode(source).decode('GBK'))
-
- #输出结果:
- b'\xc4\xe3\xba\xc3'
- 你好
- import base64
-
- with open('./test.jpg', mode='rb') as file:
- data = file.read()
- print(base64.b64encode(data).decode())
-
- import base64
- source = '/9j/4AAQSkZJRgABAQEASABIAAD/2wBAf/2Q=='
- #此处为文件内容编码后的base64代码
- data = base64.b64decode(source)
- with open('./test123.jpg', mode='wb') as file:
- file.write(data)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。