Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #17767

Re: reading multiline output

Date 2011-12-23 02:14 +0000
From MRAB <python@mrabarnett.plus.com>
Subject Re: reading multiline output
References (1 earlier) <EAC763DC-04F6-497F-8455-1CF40A0ACC2E@gmail.com> <4EF3CF8B.4060005@mrabarnett.plus.com> <FFB26576-7928-4E01-8D56-26071FDB39CE@gmail.com> <4EF3D6F8.3020203@mrabarnett.plus.com> <2D5264F3-A1EB-4C68-85E2-A39C5C67E699@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4012.1324606465.27778.python-list@python.org> (permalink)

Show all headers | View raw


On 23/12/2011 02:05, Mac Smith wrote:
>
> On 23-Dec-2011, at 6:48 AM, MRAB wrote:
>
>>  On 23/12/2011 01:07, Mac Smith wrote:
>>>
>>>  On 23-Dec-2011, at 6:17 AM, MRAB wrote:
>>>
>>>>  On 23/12/2011 00:33, Mac Smith wrote:
>>>>>  Hi,
>>>>>
>>>>>
>>>>>  I have started HandBrakeCLI using subprocess.popen but the output
>>>>>  is multiline and not terminated with \n so i am not able to read
>>>>>  it using readline() while the HandBrakeCLI is running. kindly
>>>>>  suggest some alternative. i have attached the output in a file.
>>>>>
>>>>  The lines are terminated with \r, so read with read() and then
>>>>  split on "\r".
>>>
>>>  read() will read the complete output and than i will be able to parse
>>>  it, i want to read the output of the command in realtime.
>>>
>>  Try telling it how much to read with read(size):
>>
>>  def read_lines(output, line_ending="\n"):
>>     buffer = ""
>>
>>     while True:
>>         chunk = output.read(1024)
>>         if not chunk:
>>             break
>>
>>         buffer += chunk
>>
>>         while True:
>>             pos = buffer.find(line_ending)
>>             if pos<  0:
>>                 break
>>
>>             pos += len(line_ending)
>>             yield buffer[ : pos]
>>             buffer = buffer[pos : ]
>>
>>     if buffer:
>>         yield buffer
>
> thanks, this helped. just need to correct line_ending="\n" should be line_ending="\r"
>
I wrote the default as "\n" because that's the normal line ending in
Python.

If, as in your case, the line ending is different, just pass the
appropriate string as the second argument.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: reading multiline output MRAB <python@mrabarnett.plus.com> - 2011-12-23 02:14 +0000

csiph-web