io - How to decode base64 file into binary in Python? -
i'm building system handles pdf file data (for use pypdf2 lib). obtain base64 encoded pdf can decode , store correctly using following:
import base64 # base64filedata <= base64 file data filedata = base64.urlsafe_b64decode(base64filedata.encode('utf-8')) open('thefilename.pdf', 'w') thefile: thefile.write(filedata)
i want use filedata
binary file split up, when type(filedata)
, filedata
turns out <type 'str'>
. how can convert filedata
binary (or @ least not string)?
all tips welcome!
[edit]
if open(filedata, 'rb')
error, saying
typeerror: file() argument 1 must encoded string without null bytes, not str
to remove null bytes tried, filedata.rstrip(' \t\r\n\0')
, filedata.rstrip('\0')
, filedata.partition(b'\0')[0]
, nothing seems work. ideas?
[edit2]
the thing pass string pypdf2 pdffilereader class, on lines 909 912 following (in stream
filedata
provide):
if type(stream) in (string_type, str): fileobj = open(stream, 'rb') stream = bytesio(b_(fileobj.read())) fileobj.close()
so because string, assumes filename, after tries open file. fails typeerror
. before feeding filedata
pdffilereader need somehow convert else str
doesn't try open it, considers filedata
file on itself. ideas?
hence open's binary mode have use 'wb' else gets saved "text" basically.
import base64 # base64filedata <= base64 file data filedata = base64.urlsafe_b64decode(base64filedata.encode('utf-8')) open('thefilename.pdf', 'wb') thefile: thefile.write(filedata)
Comments
Post a Comment