标准错误.write;打印字符串

发布于 2024-11-23 20:59:35 字数 415 浏览 0 评论 0原文

我是 Python 新手,在使用 stderr.write 函数时遇到一些问题。我将尝试用代码来说明它。在我这样做之前:

print "Unexpected error! File {0} could not be converted." .format(src)

但是后来我想将错误消息与其他状态消息分开,所以我尝试这样做:

sys.stderr.write "Unexpected error! File %s could not be converted." src

但这会导致错误。我也用谷歌搜索了它,但我找不到任何东西。有人可以帮我吗?如何使用 stderr.write 打印字符串 src

I am new to Python and having some trouble with the stderr.write function. I will try to illustrate it with code. Before I was doing this:

print "Unexpected error! File {0} could not be converted." .format(src)

But then I wanted to separate the error messages from other status messages so I tried doing this:

sys.stderr.write "Unexpected error! File %s could not be converted." src

But this results in error. I googled it as well but i couldn't find anything. Could anyone please help me out here. How can I print the string src using stderr.write?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-11-30 20:59:35

在 Python 2.x 中:

sys.stderr.write("Unexpected error! File %s could not be converted." % src)

或者,在 Python 2.x 和 3.x 中:

sys.stderr.write("Unexpected error! File {0} could not be converted.".format(src))

In Python 2.x:

sys.stderr.write("Unexpected error! File %s could not be converted." % src)

Or, in Python 2.x and 3.x:

sys.stderr.write("Unexpected error! File {0} could not be converted.".format(src))
旧竹 2024-11-30 20:59:35

Python 中的函数后面需要跟括号 ((...)),可选地包含参数,以便被调用。

sys.stderr.write("Unexpected error! File %s could not be converted.\n" % (src,))

Functions in Python need to be followed by parens ((...)), optionally containing arguments, in order to be invoked.

sys.stderr.write("Unexpected error! File %s could not be converted.\n" % (src,))
牛↙奶布丁 2024-11-30 20:59:35

sys.stderr.write 是一个函数,因此要调用该函数,需要在参数两边使用括号:

In [1]: src='foo'

In [2]: sys.stderr.write("Unexpected error! File %s could not be converted."%src)
Unexpected error! File foo could not be converted.

请注意,从 Python3 开始,print 也是一个函数,并且还需要括号。

sys.stderr.write is a function, so to call the function, you need to use parentheses around the argument:

In [1]: src='foo'

In [2]: sys.stderr.write("Unexpected error! File %s could not be converted."%src)
Unexpected error! File foo could not be converted.

Note that as of Python3, print is also a function and will also require parentheses.

〃温暖了心ぐ 2024-11-30 20:59:35

您错过了 (,) 和 %:

import sys
sys.stderr.write("Unexpected error! File %s could not be converted." % src)

You have missed (,) and %:

import sys
sys.stderr.write("Unexpected error! File %s could not be converted." % src)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文