Print >> sys.stdout in the form of a default output format of print, which equals print "%VALUE%"
Look at the English comment for the following code, which is the default Help for print
1234567891011121314151617 |
# coding=utf-8
import
sys, os
list1Display
=
[
‘1‘
,
‘2‘
,
‘3‘
]
list2Display
=
[
‘abc‘
,
‘def‘
,
‘rfs‘
]
while
list2Display !
=
[]:
# Prints the values to a stream, or to sys.stdout by default.
# Optional keyword arguments:
# file: a file-like object (stream); defaults to the current sys.stdout.
# sep: string inserted between values, default a space.
# end: string appended after the last value, default a newline.
# print 可以将值输出到指定的输出流(可以是文件句柄),若不指定,
# 则输出到stdout(标准输出)
# 一般我们使用的时候不加输出定向符“>>”到输出的file对象,本代码中对象是stdout
# 下面的print在stdout对象中每次输出两个值
print >> sys.stdout, list2Display.pop(), list1Display.pop()
os.system(
"pause"
)
|
Only the usage in python2.x is shown in the above, and print in 2.x cannot specify the end symbol as a different value, and the default is to output a "\ n", that is, it must be changed to the next line at a time. By the 3.x, print becomes a real function, and you can then specify the value of the end symbol arbitrarily, and you can add any value you want at the end of the output, instead of forcing a newline.
Therefore, in 2.x if you want to implement the output does not wrap, can only call the StdOut object's write method, the following is also an instance, because StdOut does not have the end of this symbol, the output will not be wrapped, so if you want to output multiple times, in the need to output the string object inside add "\ r , you can go back to the beginning of the line.
1234567 |
# coding=utf-8
import
sys, os
import time
for
i
in
range
(5
):
time.sleep( .
5
)
sys.stdout.write(
"File transfer progress :[%3d] percent complete!\r"
% i )
sys.stdout.flush()
|
Experience, will the above "\ r" take off to try, is not a line and direct output? You got it. For a long time, Python will stay in the 2.x era.
The difference between running results is as follows:
-----------------------------------------------------------------
Network excerpt notes, if involved in the version, please contact the blogger delete.
The difference between Sys.stdout.write and print >> sys.stdout in Python