Python 中的重定向打印: val = print(arg) 将混合迭代输出到文件
假设我有一个令人难以置信的嵌套迭代列表/字典。我想尽可能轻松地将它们打印到文件中。为什么我不能将打印重定向到文件?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以查看Python的 logging 模块。也许这就是这种情况下的完美匹配。
You could look into the logging module of Python. Perhaps that's the perfect match in this case.
您通常不会使用
print
写入文件(尽管技术上可以)。为此,您可以使用文件对象。如果
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.If
print
is taking forever to display a massive string, it is likely that it's not exactlyprint
's fault, but the result of having to display all that to screen.看看您如何使用
print
作为函数,文档说您可以像这样重定向到文件:Seeing how you're using
print
as function, docs say that you can redirect to file like this:在 Python
3.*
中,将一个print
调用重定向到打开的文件对象destination
,在 Python
2.*
中>,其中print
是一条语句,语法是我想象您正在使用
2.*
因为在3.*
中分配print
的结果不是语法错误(它只是无用,因为该结果是None
,但允许)。在2.*
中,print
是一个语句,而不是一个函数,所以你给出的代码片段确实是一个语法错误。我不确定这项任务的含义是什么。如果您想重定向一个或多个
print
语句(或调用)以获取内存字符串形式的格式化结果,您可以将sys.stdout
设置为>StringIO
(或cStringIO
)实例;但你特别提到“到一个文件”,所以我真的很困惑该作业的预期含义。请澄清一下?In Python
3.*
, to redirect oneprint
call to an open file objectdestination
,In Python
2.*
, whereprint
is a statement, the syntax isI imagine you're using
2.*
because in3.*
assigningprint
's result is not a syntax error (it's just useless, since that result isNone
, but allowed). In2.*
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 setsys.stdout
to aStringIO
(orcStringIO
) instance; but you specifically mention "to a file" so I'm really perplexed as to the intended meaning of that assignment. Clarify pls?我使用 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.