Python 3:接收用户输入,包括换行符 [英] Python 3: receive user input including newline characters
本文介绍了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屋!
查看全文