io module and pdf question

MRAB python at mrabarnett.plus.com
Tue Jun 25 12:59:49 EDT 2013


On 25/06/2013 17:15, jyoung79 at kc.rr.com wrote:
> Thank you Rusi and Christian!
>
> So it sounds like I should read the pdf data in as binary:
>
> --------------------
> import os
>
> pdfPath = '~/Desktop/test.pdf'
>
> colorlistData = ''
>
> with open(os.path.expanduser(pdfPath), 'rb') as f:
>      for i in f:
>          if 'XYZ:colorList' in i:
>              colorlistData = i.split('XYZ:colorList')[1]
>              break
>
> print(colorlistData)
> --------------------
>
> This gives me the error:
> TypeError: Type str doesn't support the buffer API
>
> I admit I know nothing about binary, except it's ones and zeroes.  Is there a way to read it in as binary, convert it to ascii/unicode, and then somehow split it by newline characters so that I can pull the appropriate metadata lines out?  For example, XYZ:colorList="DarkBlue,Yellow"
>
In Python 2, string literals like '' are by default bytestrings. If you
want a Unicode string you need to add the prefix u, so u''.

In Python 3, string literals like '' are by default Unicode. If you
want a bytestring you need to add the prefix b, so b''.

Python 2 was lax when mixing bytestrings with Unicode strings.

Python 3, on the other hand, insists that you know the difference: is
it text (Unicode) or binary data (bytestring)?

> Thanks!
>
> Jay
>
> --
>
>> Most of the PDF objects are therefore not encoded. It is, however,
>> possible to include a PDF into another PDF and to encode it, but that's
>> a rare case. Therefore the metadata can usually be read in text mode.
>> However, to correctly find all objects, the xref-table indexes offsets
>> into the PDF. It must be treated binary in any case, and that's the
>> funny reason for the first 3 characters of the PDF - they must include
>> characters with the 8th bit set, such that FTP applications treat it as
>> binary.
>
>> 	Christian
>




More information about the Python-list mailing list