“打印>>"是什么意思?在 python 中做什么? [英] What does "print >>" do in python?

查看:107
本文介绍了“打印>>"是什么意思?在 python 中做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将代码从 python 2 翻译成 python 3,但我不明白 print >> 做了什么以及我应该如何在 python 3 中编写它.

I have to translate a code from python 2 into python 3 and I can't understand what does print >> do and how should I write it in python 3.

print >> sys.stderr, '--'
print >> sys.stderr, 'entrada1: ', entrada1
print >> sys.stderr, 'entrada2: ', entrada2
print >> sys.stderr, '--'

推荐答案

>>sys.stderr 部分使 print 语句输出到 stderr 而不是 Python 2 中的 stdout.

The >> sys.stderr part makes the print statement output to stderr instead of stdout in Python 2.

引用文档:

print 也有一个扩展形式,由第二部分定义上面描述的语法.这种形式有时被称为打印雪佛龙."在这种形式中,>>> 之后的第一个表达式必须评估为类文件"对象,特别是具有write() 方法如上所述.通过这种扩展形式,后续表达式将打印到此文件对象.如果第一个表达式计算为 None,然后 sys.stdout 用作文件输出.

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 中,将 file 参数用于 print() 函数:

In Python 3 use the file argument to the print() function:

 print("spam", file=sys.stderr)

这篇关于“打印>>"是什么意思?在 python 中做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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