How to pipe input to python line by line from linux program?

前端 未结 3 1353
故里飘歌
故里飘歌 2020-11-30 22:21

I want to pipe the output of ps -ef to python line by line.

The script I am using is this (first.py) -

#! /usr/bin/python

import sys         


        
3条回答
  •  一个人的身影
    2020-11-30 23:14

    Another approach is to use the input() function (the code is for Python 3).

    while True:
            try:
                line = input()
                print('The line is:"%s"' % line)
            except EOFError:
                # no more information
                break
    

    The difference between the answer and the answer got by Dr. Jan-Philip Gehrcke is that now each of the lines is without a newline (\n) at the end.

提交回复
热议问题