核心代码:
import csv
import base64
def image_to_base64():
'''封装把图片转换为base64编码格式'''
o = open(r'E:Jmeter1.jpg', 'rb')
base64_data = base64.b64encode(o.read())
s = base64_data.decode()
return ("data:image/png;base64,%s"%s)
def base64_write_csv():
'''把生成的base64写入CSV文件'''
f = open(r'E:Jmeterimage.csv', 'wb')
csv_writer = csv.writer(f)
csv_writer.writerow(['image'])
csv_writer.writerow([image_to_base64()])
f.close()
if __name__ == '__main__':
base64_write_csv()
报错如下:
注意点:
一定要将str转为bytes
python36把file方法改为open
open('xxx.csv', 'wb')将wb改为w就没报错了