交互模式下 sys.stdout 的输出 [英] Output from sys.stdout in interactive mode
问题描述
我在交互模式下测试了 sys.stdout.write;为什么我的数字后缀是额外"的 1 和 2?如果我从文件运行代码,我会在 Windows 机器上得到预期的输出 (1234 ...) Python 3.3
<预><代码>>>>导入系统>>>对于范围内的 i (15):... sys.stdout.write(str(i))...01112131415161718191102112122132142>>>Python also 回显 sys.stdout.write()
调用的返回值,即写入的字节数:
这里的下一个提示跟在没有换行符的 '10'
后面.
或者,作为一种不同的演示方式,在循环中写入 0 个字节会多次打印 0
:
I tested sys.stdout.write in interactive mode; why do I get the 'extra' 1 and 2 suffixed to the numbers? If I run the code from a file I get the expected output (1234...) Python 3.3 on a Windows machine
>>> import sys
>>> for i in range(15):
... sys.stdout.write(str(i))
...
01
11
21
31
41
51
61
71
81
91
102
112
122
132
142
>>>
Python is also echoing the return value of sys.stdout.write()
call, which is the number of bytes written:
>>> import sys
>>> written = sys.stdout.write('10')
10>>> written
2
Here the next prompt follows the '10'
written without a newline.
Or, as a different way of demoing, writting 0 bytes in a loop prints 0
that many times:
>>> for i in range(3):
... sys.stdout.write('')
...
0
0
0
这篇关于交互模式下 sys.stdout 的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!