4

I think I can use from J.F. Sebastian's answer in another thread to read from a subprocess line by line, using a line buffer

p = Popen(["command", "args"], stdout=PIPE, bufsize=1)
with p.stdout:
    for line in iter(p.stdout.readline, b''):
        print(line, end='\n')

The problem I'm trying to solve is my output doesn't have line breaks, but uses special characters (such as "#") to indicate termination, which I want to treat as line breaks when printing.

Community
  • 1
  • 1
Shitong
  • 43
  • 1
  • 4
  • You'll have to buffer the output yourself. Might be helpful to post some sample output from your process, and how you'd like to actually display it. – Wayne Werner Apr 18 '16 at 23:18
  • 1
    1- As the answer that you've linked indicates, you could use `for line in p.stdout` on Python 3 (as hinted by the `print()` function in your code) instead of `iter(..)` 2- To match the stream against a regex, [`python-streamexpect` package](https://github.com/digidotcom/python-streamexpect/issues/3) provides an interface similar to `pexpect`. Related: [Reading a file with a specified delimiter for newline](http://stackoverflow.com/q/16260061/4279) (`p.stdout` is file-like) – jfs Apr 19 '16 at 15:02

1 Answers1

3

You could read a char at a time checking for delimiters:

p = Popen(["command", "args"], stdout=PIPE, universal_newlines=True)
tmp = ""
delims = {"#", "!"}
for ch in iter(lambda: p.stdout.read(1), ""):
    if ch in delims:
        print(tmp)
        tmp = ""
    else:
        tmp += ch

Not the prettiest solution but if you have multiple delimiters I don't see many other ways to do it.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321