Python 3:接收包括换行符的用户输入 [英] Python 3: receive user input including newline characters

查看:36
本文介绍了Python 3:接收包括换行符的用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Python 3的命令行中读取以下文本(逐字复制,换行和所有内容):

I'm trying to read in the following text from the command-line in Python 3 (copied verbatim, newlines and all):

lcbeika
rraobmlo
grmfina
ontccep
emrlin
tseiboo
edosrgd
mkoeys
eissaml
knaiefr

使用 input ,我只能读取第一个单词,因为它读取了它停止读取的第一个换行符.

Using input, I can only read in the first word as once it reads the first newline it stops reading.

有没有一种方法可以读取全部内容而无需迭代调用 input ?

Is there a way I could read in them all without iteratively calling input?

推荐答案

您可以 import sys 并使用 sys.stdin 上的方法,例如:

You can import sys and use the methods on sys.stdin for example:

text = sys.stdin.read()

或:

lines = sys.stdin.readlines()

或:

for line in sys.stdin:
    # Do something with line.

这篇关于Python 3:接收包括换行符的用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆