如何为SYS.STDIN分配特定值



我正在使用以下代码:

#!/usr/bin/env python
import sys
from io import StringIO
#data = sys.stdin.readlines()
sys.stdin = """
hello I feel good
how are you?
where have you been?
"""
for line in sys.stdin:
    print line

运行上述代码时,打印行会打印出分配给sys.stdin的文本的每个字符。它每行打印一个字符:

h
e
l
l
o
I
....truncated

我试图使输出存储在sys.stdin中,应该看起来像这样:

hello I feel good
how are you?
where have you been?

这似乎有效:

from io import StringIO
import sys
data = u"""
hello I feel good
how are you?
where have you been?
"""
sys.stdin = StringIO(data)
for line in sys.stdin:
    print line.rstrip()

输出:

hello I feel good
how are you?
where have you been?

dabba.txt文件的内容:

hello I feel good
how are you?
where have you been?

这是del.py文件中的代码

import sys
datq=sys.stdin.readlines()
for line in datq:
    print(line)

来自Ubuntu 18.04的命令行:

cat dabba.txt | python del.py 

上述代码的输出是:

hello I feel good
how are you?
where have you been?

最新更新