你只需要反覆sys.stdin
:
[email protected]:~/Work/playground$ cat pipein.py
import sys
def main():
for line in sys.stdin:
print "line '%s'" % line.rstrip("\n")
if __name__ == "__main__":
main()
[email protected]:~/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}
[email protected]:~/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}'
'在sys.stdin行:打印(線)' –
我已經通過[其它問題](HTTP的答案了://計算器.com/questions/1450393/how-do-you-read-from-stdin-in-python)。問題更多的是流媒體內容。如果我通過網絡檢索內容,其中內容以塊的形式出現在流中,python腳本會等待內容是否可用? –
在發佈之前你有沒有打擾過呢? –