I need to do something in Python 2.7 that I'm able to to in Python 3.
This code works in Python 3 to write an array to PDF.
PDFbyteArray = [37, 80, 68, 70...]
with open('NewPDFfile.pdf', 'wb') as binary_file:
binary_file.write(bytes(PDFbyteArray))
When I try to do the same thing in python 2 it doesn't convert the array to bytes. From what I have read online the bytes() function is a python 3 thing. So my question is how do I achieve the same result in python 2?
CodePudding user response:
In Python 2.6 , to convert a list of integers to what you need (an array of bytes), use the built-in bytearray() class:
PDFbyteArray = [37, 80, 68, 70]
with open('NewPDFfile.pdf', 'wb') as binary_file:
binary_file.write(bytearray(PDFbyteArray))
print('fini')
