python 报错:a bytes-like object is required, not 'str'

核心代码:
import csv
import base64

def image_to_base64():
'''封装把图片转换为base64编码格式'''
o = open(r'E:\Jmeter\1.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:\Jmeter\image.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就没报错了
posted @ 2020-12-29 14:20  彬哥哥开始躁动  阅读(1627)  评论(0编辑  收藏  举报