Re: Python3: Reading a text/binary mixed file

2015-03-12 Thread Paulo da Silva
On 11-03-2015 01:09, Cameron Simpson wrote: > On 10Mar2015 22:38, Paulo da Silva wrote: >> On 10-03-2015 04:14, Cameron Simpson wrote: ... > > Since binary files (returning bytes from reads) also have a convenient > readline method looking for byte 10 ('\n') this makes you current task > tractab

Re: Python3: Reading a text/binary mixed file

2015-03-10 Thread Cameron Simpson
On 10Mar2015 22:38, Paulo da Silva wrote: On 10-03-2015 04:14, Cameron Simpson wrote: On 10Mar2015 04:01, Paulo da Silva wrote: But this is very tricky! I am on linux, but if I ran this program on windows I needed to change it to "eat" also the '\r'. If you're in Python 3 (recommended!) and

Re: Python3: Reading a text/binary mixed file

2015-03-10 Thread Paulo da Silva
On 10-03-2015 05:56, Steven D'Aprano wrote: ... Thank you very much for your post. I learned what I'm needing from it! Exactly what I was looking for. Thank you. -- https://mail.python.org/mailman/listinfo/python-list

Re: Python3: Reading a text/binary mixed file

2015-03-10 Thread Paulo da Silva
On 10-03-2015 12:41, random...@fastmail.us wrote: > On Tue, Mar 10, 2015, at 00:01, Paulo da Silva wrote: >> For .pnm photo files I read the entire file (I needed it in memory >> anyway), splited a copy separated by b'\n', got the headers stuff and >> then used the original remaining bytes as the p

Re: Python3: Reading a text/binary mixed file

2015-03-10 Thread Paulo da Silva
On 10-03-2015 04:14, Cameron Simpson wrote: > On 10Mar2015 04:01, Paulo da Silva wrote: >> On 10-03-2015 00:55, Dave Angel wrote: ... >> For .pnm photo files I read the entire file (I needed it in memory >> anyway), splited a copy separated by b'\n', got the headers stuff and >> then used the orig

Re: Python3: Reading a text/binary mixed file

2015-03-10 Thread random832
On Tue, Mar 10, 2015, at 00:01, Paulo da Silva wrote: > For .pnm photo files I read the entire file (I needed it in memory > anyway), splited a copy separated by b'\n', got the headers stuff and > then used the original remaining bytes as the photo pixels. > But this is very tricky! I am on linux,

Re: Python3: Reading a text/binary mixed file

2015-03-09 Thread Steven D'Aprano
Paulo da Silva wrote: > Hi! > > What is the best way to read a file that begins with some few text lines > and whose rest is a binary stream? > > As an exmaple ... files .pnm. > > Thanks for any comments/help on this. A mixed text/binary file is really a binary file that contains some binary

Re: Python3: Reading a text/binary mixed file

2015-03-09 Thread Cameron Simpson
On 10Mar2015 04:01, Paulo da Silva wrote: On 10-03-2015 00:55, Dave Angel wrote: On 03/09/2015 08:45 PM, Paulo da Silva wrote: What is the best way to read a file that begins with some few text lines and whose rest is a binary stream? [...] Generally speaking, you can treat a piece of a bina

Re: Python3: Reading a text/binary mixed file

2015-03-09 Thread Paulo da Silva
On 10-03-2015 00:56, Chris Angelico wrote: > On Tue, Mar 10, 2015 at 11:45 AM, Paulo da Silva > wrote: >> Hi! >> ... > Read the entire file in binary mode, and figure out which parts are > text and how they're encoded (possibly ASCII or UTF-8). Then take just > those snippets, and decode them. So

Re: Python3: Reading a text/binary mixed file

2015-03-09 Thread Paulo da Silva
On 10-03-2015 00:55, Dave Angel wrote: > On 03/09/2015 08:45 PM, Paulo da Silva wrote: >> Hi! >> >> What is the best way to read a file that begins with some few text lines >> and whose rest is a binary stream? >> ... > > In which version of Python? there's a huge difference between 2.x and 3.x

Re: Python3: Reading a text/binary mixed file

2015-03-09 Thread Chris Angelico
On Tue, Mar 10, 2015 at 12:01 PM, Dave Angel wrote: >> data = open("some_file", "rb") > > > You accidentally omitted the part where you read() the data > > data = data.read() You know, that would help... a LOT. :) ChrisA -- https://mail.python.org/mailman/listinfo/python-list

Re: Python3: Reading a text/binary mixed file

2015-03-09 Thread Dave Angel
On 03/09/2015 08:56 PM, Chris Angelico wrote: On Tue, Mar 10, 2015 at 11:45 AM, Paulo da Silva wrote: Hi! What is the best way to read a file that begins with some few text lines and whose rest is a binary stream? As an exmaple ... files .pnm. Thanks for any comments/help on this. Read the

Re: Python3: Reading a text/binary mixed file

2015-03-09 Thread Chris Angelico
On Tue, Mar 10, 2015 at 11:45 AM, Paulo da Silva wrote: > Hi! > > What is the best way to read a file that begins with some few text lines > and whose rest is a binary stream? > > As an exmaple ... files .pnm. > > Thanks for any comments/help on this. Read the entire file in binary mode, and figu

Re: Python3: Reading a text/binary mixed file

2015-03-09 Thread Dave Angel
On 03/09/2015 08:45 PM, Paulo da Silva wrote: Hi! What is the best way to read a file that begins with some few text lines and whose rest is a binary stream? As an exmaple ... files .pnm. Thanks for any comments/help on this. In which version of Python? there's a huge difference between 2.