AttributeError:“字节"对象没有属性“编码"; base64编码pdf文件 [英] AttributeError: 'bytes' object has no attribute 'encode'; base64 encode a pdf file

查看:383
本文介绍了AttributeError:“字节"对象没有属性“编码"; base64编码pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对python中的pdf进行base64编码.几个这样的答案对其他人还是有用的,但由于某种原因,对我而言并没有奏效.我最近的尝试是:

I am trying to base64 encode a pdf in python. Several SO answers to this worked for other people but not on my end for some reason. My most recent attempt is:

# http://stackoverflow.com/questions/12020885/python-converting-file-to-base64-encoding
with open('/home/cchilders/projects/myproject/data/books/software-and-mind.pdf', 'rb') as f:
    encoded = f.read().encode("base64")
    print(encoded)

我知道

AttributeError: 'bytes' object has no attribute 'encode'

如何将这个pdf文件设为base64?谢谢

How can I base64 this pdf file? Thank you

推荐答案

您应该为此使用base64模块

you should use the base64 module for this

import base64
base64.b64encode(f.read())

这篇关于AttributeError:“字节"对象没有属性“编码"; base64编码pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆