2

I have a log file on my server and I am using a CLI Program to fetch the content to terminal. I need to do a bit of filtering and json operation and I am more comfortable in doing that in python rather than in some bash script. Now my question is is there a way to pipe the stream to python?

something like this

cliProgram fetchLogs | python script.py 

In Python, I want to parse the content line by line so python file should have a way to read the data line by line and if data is not available (may be because of network delay) , it should wait for more data and exit only when the stream is closed.

5
  • for line in sys.stdin: print(line) Commented May 11, 2017 at 10:14
  • I have already gone through the answers in other question . The question was more towards the streaming the content. If I am retrieving the content over wire, where the content comes as a stream in chunks, will the python script wait until the content is available? Commented May 11, 2017 at 10:20
  • 1
    Did you bother trying before posting here ? Commented May 11, 2017 at 10:23
  • Yes, it will block unless you use asyncio. Commented May 11, 2017 at 10:24
  • This isn't a Python thing, it's a shell thing. This is how pipelines always work, for all programs reading from stdin/writing to stdout. Having said that, the shell is a platform feature, so you should probably mention which platform you're using. Commented May 11, 2017 at 10:27

1 Answer 1

4

You just have to iterate on sys.stdin :

bruno@bigb:~/Work/playground$ cat pipein.py
import sys

def main():
    for line in sys.stdin:
        print "line '%s'" % line.rstrip("\n")

if __name__ == "__main__":
    main()

bruno@bigb:~/Work/playground$ cat wotdata.txt 
E = 0
m = 1
J = 3
K = 2
p = {0: 0.696969696969697, 1: 0.30303030303030304}
UDC = {(0, 1): 9.0, (1, 2): 10.0, (0, 0): 5.0, (1, 1): 6.0, (1, 0): 9.0, (0, 2): 6.0}
UDU = {(0, 1): 5.0, (1, 2): 4.0, (0, 0): 2.0, (1, 1): 4.0, (1, 0): 1.0, (0, 2): 3.0}
UAC = {(0, 1): 1.0, (1, 2): 0.0, (0, 0): 2.0, (1, 1): 3.0, (1, 0): 4.0, (0, 2): 0.0}
UAU = {(0, 1): 9.0, (1, 2): 10.0, (0, 0): 5.0, (1, 1): 6.0, (1, 0): 10.0, (0, 2): 6.0}

bruno@bigb:~/Work/playground$ cat wotdata.txt | python pipein.py
line 'E = 0'
line 'm = 1'
line 'J = 3'
line 'K = 2'
line 'p = {0: 0.696969696969697, 1: 0.30303030303030304}'
line 'UDC = {(0, 1): 9.0, (1, 2): 10.0, (0, 0): 5.0, (1, 1): 6.0, (1, 0): 9.0, (0, 2): 6.0}'
line 'UDU = {(0, 1): 5.0, (1, 2): 4.0, (0, 0): 2.0, (1, 1): 4.0, (1, 0): 1.0, (0, 2): 3.0}'
line 'UAC = {(0, 1): 1.0, (1, 2): 0.0, (0, 0): 2.0, (1, 1): 3.0, (1, 0): 4.0, (0, 2): 0.0}'
line 'UAU = {(0, 1): 9.0, (1, 2): 10.0, (0, 0): 5.0, (1, 1): 6.0, (1, 0): 10.0, (0, 2): 6.0}'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.