加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何为sys.stdin分配特定值?

发布时间:2020-12-17 17:38:41 所属栏目:Python 来源:网络整理
导读:我正在使用以下代码: #!/usr/bin/env pythonimport sysfrom io import StringIO#data = sys.stdin.readlines()sys.stdin = """hello I feel goodhow are you?where have you been?"""for line in sys.stdin: print line 当我运行上述代码时,打印行将打印出

我正在使用以下代码:

#!/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?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读