sys.stdin.readline() [英] sys.stdin.readline()

查看:104
本文介绍了sys.stdin.readline()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在for循环中调用readline()时,为什么它返回一系列

一个char字符串,而不是整行?

When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?

:print sL



....

abc

a

b

c


(我输入了''abc'',循环打印出来''a \ nnb \ nc \''')


即如何在返回

值之前让readline()等待换行符? ''readline()''在这里似乎表现得像''read()''。


(''readlines()''在这种情况下工作正常,除了它等待

eof;我真的宁愿迭代stdin中的行,因为它们进来了)


....
abc
a
b
c

(I typed in ''abc'', and the loop printed out ''a\nb\nc\n'')

I.e. how can I make readline() wait for the newline before returning a
value? ''readline()'' seems to be acting exactly like ''read()'' here.

(''readlines()'' works fine in this context, except that it waits for
eof; I''d really rather iterate over lines in stdin as they come in)

推荐答案

2004-08-31,Mike Maxwell< ma ***** @ ldc.upenn.edu>写道:
On 2004-08-31, Mike Maxwell <ma*****@ldc.upenn.edu> wrote:
当我在for循环中调用readline()时,为什么它会返回一系列单字符串而不是整行?
When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?
for sys.stdin.readline()中的sL:print sL
for sL in sys.stdin.readline(): print sL

...
abc
a
b
c

(我输入''abc'',循环打印出来''a\\\
b\\\
c\ n'')
<是的如何在返回
值之前让readline()等待换行符?

...
abc
a
b
c

(I typed in ''abc'', and the loop printed out ''a\nb\nc\n'')

I.e. how can I make readline() wait for the newline before returning a
value?




它是。

''readline ()''似乎表现得像''read()''在这里。



It is.
''readline()'' seems to be acting exactly like ''read()'' here.




排序。


你想要的是:


导入系统

而True:

s = sys.stdin.readline()

如果不是s:

休息

打印s


-

格兰特爱德华兹格兰特哇! ..我在SEATS看到厕所

...

visi.com



Sort of.

What you want is:

import sys
while True:
s = sys.stdin.readline()
if not s:
break
print s

--
Grant Edwards grante Yow! .. I see TOILET
at SEATS...
visi.com


Mike Maxwell写道:
Mike Maxwell wrote:
当我在for循环中调用readline()时,为什么它会返回一系列单字符串而不是整行?
When I invoke readline() in a for loop, why does it return a series of
one-char strings, rather than the full line?
对于sys.stdin.readline()中的sL:print sL
for sL in sys.stdin.readline(): print sL




它确实返回一整行。 *一条线。然后你的循环对该行中的字符迭代



在sys.stdin.xreadlines()中尝试`for sL:print sL''。

或者在更新的Pythons中,只需`s.sys.stdin中的sL:print sL''。


-

Hallvard



It does return a full line. *One* line. Then your loop iterates
over the characters in that line.

Try `for sL in sys.stdin.xreadlines(): print sL''.
Or in newer Pythons, simply `for sL in sys.stdin: print sL''.

--
Hallvard


Hallvard B Furuseth< hbfuruseth< at> usit.uio.no>写道:
Hallvard B Furuseth <h.b.furuseth <at> usit.uio.no> writes:
或者在较新的Pythons中,只需`对于sys.stdin中的sL:print sL''。
Or in newer Pythons, simply `for sL in sys.stdin: print sL''.




这不是在交互式shell中为我工作:

Python 2.4a2(#55,2004年8月5日,11:42:43)[MSC v.1310 32位(英特尔)]在win32上

键入help,copyright,credit等。或许可证或有关更多信息。



This doesn''t work for me in the interactive shell:

Python 2.4a2 (#55, Aug 5 2004, 11:42:43) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.

在sys.stdin中导入sys
import sys
for l in sys.stdin:



.... print repr(l)

....

A

B

C

D

^ Z

''A \ n''

''B'h''

''C'h''

''D'\\ n'


请注意,在我点击^ Z之前我没有输出(显然,这是一个Windows

框)。我尝试用-u选项启动Python,我仍然得到这个

结果。有没有什么方法可以让你在'sys.stdin'中工作'

建议它在交互式提示下工作?


Steve


.... print repr(l)
....
A
B
C
D
^Z
''A\n''
''B\n''
''C\n''
''D\n''

Note that I don''t get output until I hit ^Z (this is, obviously, a Windows
box). I tried starting Python with the -u option too, and I still get this
result. Is there any way to make ''for l in sys.stdin'' work the way you
suggest it does at an interactive prompt?

Steve


这篇关于sys.stdin.readline()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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