Python 中的重定向打印: val = print(arg) 将混合迭代输出到文件

发布于 2024-08-25 17:46:38 字数 241 浏览 7 评论 0原文

假设我有一个令人难以置信的嵌套迭代列表/字典。我想尽可能轻松地将它们打印到文件中。为什么我不能将打印重定向到文件?

val = print(arg) 得到一个语法错误。

有没有办法访问标准输入?

为什么打印大量字符串会花费很长时间?我这边的编程很糟糕,无法输出大量字符串,但调试速度很快——这不是利用了交互式提示的优势吗?

可能还有比我的抱怨更简单的方法。蜂巢思维有答案吗?

So lets say I have an incredibly nested iterable of lists/dictionaries. I would like to print them to a file as easily as possible. Why can't I just redirect print to a file?

val = print(arg)
gets a SyntaxError.

Is there a way to access stdinput?

And why does print take forever with massive strings? Bad programming on my side for outputting massive strings, but quick debugging--and isn't that leveraging the strength of an interactive prompt?

There's probably also an easier way than my gripe. Has the hive-mind an answer?

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

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

发布评论

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

评论(5

百合的盛世恋 2024-09-01 17:46:38

您可以查看Python的 logging 模块。也许这就是这种情况下的完美匹配。

You could look into the logging module of Python. Perhaps that's the perfect match in this case.

霓裳挽歌倾城醉 2024-09-01 17:46:38

您通常不会使用 print 写入文件(尽管技术上可以)。为此,您可以使用文件对象。

with open(filename, 'w') as f:
    f.write(repr(your_thingy))

如果 print 需要很长时间才能显示大量字符串,则很可能这并不完全是 print 的错误,而是必须将所有内容显示到屏幕上的结果。

You don't typically use print for writing to a file (though you technically can). You would use a file object for this.

with open(filename, 'w') as f:
    f.write(repr(your_thingy))

If print is taking forever to display a massive string, it is likely that it's not exactly print's fault, but the result of having to display all that to screen.

安静 2024-09-01 17:46:38

看看您如何使用 print 作为函数,文档说您可以像这样重定向到文件

print(arg, file=open('fname', 'w'))

Seeing how you're using print as function, docs say that you can redirect to file like this:

print(arg, file=open('fname', 'w'))
属性 2024-09-01 17:46:38

在 Python 3.* 中,将一个 print 调用重定向到打开的文件对象 destination

print(arg, file=destination)

在 Python 2.* 中>,其中 print 是一条语句,语法是

print>>destination, arg

我想象您正在使用 2.* 因为在 3.* 中分配 print 的结果不是语法错误(它只是无用,因为该结果是 None,但允许)。在2.*中,print是一个语句,而不是一个函数,所以你给出的代码片段确实是一个语法错误。

我不确定这项任务的含义是什么。如果您想重定向一个或多个 print 语句(或调用)以获取内存字符串形式的格式化结果,您可以将 sys.stdout 设置为 >StringIO(或cStringIO)实例;但你特别提到“到一个文件”,所以我真的很困惑该作业的预期含义。请澄清一下?

In Python 3.*, to redirect one print call to an open file object destination,

print(arg, file=destination)

In Python 2.*, where print is a statement, the syntax is

print>>destination, arg

I imagine you're using 2.* because in 3.* assigning print's result is not a syntax error (it's just useless, since that result is None, but allowed). In 2.* print is a statement, not a function, so the code snippet you give is indeed a syntax error.

I'm not sure what the assignment is supposed to mean. If you want to redirect one or more print statements (or calls) to get the formatted result as an in-memory string, you can set sys.stdout to a StringIO (or cStringIO) instance; but you specifically mention "to a file" so I'm really perplexed as to the intended meaning of that assignment. Clarify pls?

清秋悲枫 2024-09-01 17:46:38

我使用 python 2.7 尝试了两个示例,但都没有成功

首先,我使用记事本在 C:\goat.text 中创建了一个文件

接下来,我尝试了以下

import sys

print>>"C:\ goat.txt", "测试"
错误
AttributeError: 'str' 对象没有属性 'write'

print("test", file=open('C:\goat.txt', 'w'))

SyntaxError: ("没有可行的替代方案在输入 '='", ('C:\Users\mike\AppData\Local\Temp\sikuli-tmp5165417708161227735.py', 3, 18, 'print("test", file=open(\'C:\\ goat.txt\', \'w\')) \n'))

我尝试了多种变体,但无法解决此问题。

I tried both examples with no success using python 2.7

To begin I created a file in C:\goat.text using notepad

Next I tried the following

import sys

print>>"C:\goat.txt", "test"
error
AttributeError: 'str' object has no attribute 'write'

print("test", file=open('C:\goat.txt', 'w'))

SyntaxError: ("no viable alternative at input '='", ('C:\Users\mike\AppData\Local\Temp\sikuli-tmp5165417708161227735.py', 3, 18, 'print("test", file=open(\'C:\\goat.txt\', \'w\')) \n'))

I've tried multiple variants annd can't solve this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文