在这个例子中我不能脱离for循环: ------ import sys lst = [] for sys.stdin中的行: lst.append(line) break 打印lst ----------- 但是,我可以打破我执行此操作时不在for循环中: --------- 导入系统 lst = [] for line in open(" aaa.txt"): lst.append(line) 打破 打印lst ---------- 为什么不在第一个例子中打破工作?
I can''t break out of the for loop in this example: ------ import sys lst = [] for line in sys.stdin: lst.append(line) break print lst ----------- But, I can break out of the for loop when I do this: --------- import sys lst = [] for line in open("aaa.txt"): lst.append(line) break print lst ---------- Why doesn''t break work in the first example?
推荐答案7stud schrieb: 7stud schrieb: 在这个例子中我不能脱离for循环: ------ import sys lst = [] : lst.append(行) break 打印lst ----------- I can''t break out of the for loop in this example: ------ import sys lst = [] for line in sys.stdin: lst.append(line) break print lst -----------
适合我。但只有在使用C-d关闭stdin之后。 我认为这是操作系统的事情。第一行不会传递给 进程,直到文件关闭 - C-d - 或OS填充之前操作系统 的缓冲区。你可以以某种方式切换到无缓冲的behviour ,google for it。 Termios应该在你的查询中。 无论哪种方式,它都不是python表现不同。 Diez
Works for me. But only after the stdin is closed with a C-d. I presume this is an OS thing. The first lines aren''t communicated to the process until either the file is closed - C-d - or the buffer the OS puts before the stream is filled. You can switch to unbuffered behviour somehow, google for it. Termios should be in your query. Either way, it''s not python behaving differently. Diez
2007年4月12日上午3:20,7stud写道: On Apr 12, 2007, at 3:20 AM, 7stud wrote: 我可以在这个例子中,我没有摆脱for循环: ------ import sys lst = [] for sys.stdin中的行: lst.append(行) break b $ b打印lst ----------- 但是,我可以打破for循环当我这样做时: --------- import sys lst = [] for line in open(" aaa.txt"): lst.append(line) break 打印lst ---------- 为什么不打破第一个工作例? I can''t break out of the for loop in this example: ------ import sys lst = [] for line in sys.stdin: lst.append(line) break print lst ----------- But, I can break out of the for loop when I do this: --------- import sys lst = [] for line in open("aaa.txt"): lst.append(line) break print lst ---------- Why doesn''t break work in the first example?
我的猜测是因为你没有进入EOF。尝试输入^ D(或 ,如果你在Windows上,我认为它是^ Z),同时使用你的第一个例子 ,看看它做了什么。 br />
My guess is because you''ve not entered an EOF. Try entering ^D (or if you''re on Windows I think it''s ^Z) while using your first example and see what it does.
En Thu,2007年4月12日05:20:58 -0300,7stud< bb ********** @ yahoo> $ b $bescribió: En Thu, 12 Apr 2007 05:20:58 -0300, 7stud <bb**********@yahoo> escribió: 在这个例子中我不能脱离for循环: ------ 导入系统 lst = [] for sys.stdin中的行: lst.append(行) 休息 打印lst -------- --- I can''t break out of the for loop in this example: ------ import sys lst = [] for line in sys.stdin: lst.append(line) break print lst -----------
Python 2.5.1c1(r251c1:54692,2007年4月5日,09:19:18)[MSC v.1310 32 bit (英特尔)] win32上 输入help,copyright,credit等等。或许可证或更多信息。 pyimport sys pylst = [] sys.stdin中的pyfor行: 。 ... lst.append(线) ....休息 .... hola que tal como estan ^ Z pyprint lst b $ b [''hola \ n''] ----------- 我打字很多行,但是lst只包含一个项目,正如预期的那样。与常规文件示例相同 :文件包含许多行,但只有 首先进入列表。 - Gabriel Genellina
Python 2.5.1c1 (r251c1:54692, Apr 5 2007, 09:19:18) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. pyimport sys pylst = [] pyfor line in sys.stdin: .... lst.append(line) .... break .... hola que tal como estan ^Z pyprint lst [''hola\n''] ----------- I typed many lines, but lst contains only one item, as expected. Same as your regular file example: the file contains many lines, but only the first goes into the list. -- Gabriel Genellina
更多推荐
从sys.stdin读取
发布评论