Python AttributeError: 'bytes' 对象没有属性 'encode'; base64 编码 pdf 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36228117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AttributeError: 'bytes' object has no attribute 'encode'; base64 encode a pdf file
提问by codyc4321
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:
我正在尝试在 python 中对 pdf 进行 base64 编码。对此的几个 SO 答案对其他人有用,但出于某种原因对我而言却没有。我最近的尝试是:
# 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)
I get
我得到
AttributeError: 'bytes' object has no attribute 'encode'
How can I base64 this pdf file? Thank you
我怎样才能base64这个pdf文件?谢谢
回答by Joran Beasley
you should use the base64 module for this
您应该为此使用 base64 模块
import base64
base64.b64encode(f.read())