"print >>" 在 python 中做什么?
What does "print >>" do in python?
我必须将 python 2 中的代码翻译成 python 3,我无法理解 print >>
的作用以及我应该如何在 [=16= 中编写它] 3.
print >> sys.stderr, '--'
print >> sys.stderr, 'entrada1: ', entrada1
print >> sys.stderr, 'entrada2: ', entrada2
print >> sys.stderr, '--'
>> sys.stderr
部分使 print
语句输出到 stderr 而不是 Python 中的 stdout 2.
print
also has an extended form, defined by the second portion of the
syntax described above. This form is sometimes referred to as “print
chevron.” In this form, the first expression after the >>
must
evaluate to a “file-like” object, specifically an object that has a
write()
method as described above. With this extended form, the
subsequent expressions are printed to this file object. If the first
expression evaluates to None
, then sys.stdout
is used as the file for
output.
在Python 3中使用print()
函数的file
参数:
print("spam", file=sys.stderr)
要将这些从 Python 2 转换为 Python 3,请更改:
print >>sys.stderr, 'Hello'
至:
print('Hello', file=sys.stderr)
用于打印到 stderr
便笺
sys.stderr.write()
可跨版本移植,但与print
不同,您需要添加换行符;例如
import sys
errlog = sys.stderr.write
errlog("an error message\n")
我必须将 python 2 中的代码翻译成 python 3,我无法理解 print >>
的作用以及我应该如何在 [=16= 中编写它] 3.
print >> sys.stderr, '--'
print >> sys.stderr, 'entrada1: ', entrada1
print >> sys.stderr, 'entrada2: ', entrada2
print >> sys.stderr, '--'
>> sys.stderr
部分使 print
语句输出到 stderr 而不是 Python 中的 stdout 2.
>>
must evaluate to a “file-like” object, specifically an object that has awrite()
method as described above. With this extended form, the subsequent expressions are printed to this file object. If the first expression evaluates toNone
, thensys.stdout
is used as the file for output.
在Python 3中使用print()
函数的file
参数:
print("spam", file=sys.stderr)
要将这些从 Python 2 转换为 Python 3,请更改:
print >>sys.stderr, 'Hello'
至:
print('Hello', file=sys.stderr)
用于打印到 stderr
便笺
sys.stderr.write()
可跨版本移植,但与print
不同,您需要添加换行符;例如
import sys
errlog = sys.stderr.write
errlog("an error message\n")