简体   繁体   English

如何重定向输出<None>在python中将对象输入到文本文件?

[英]How to redirect output from <None> type object to text file in python?

I am struck in one problem.I am trying to redirect the output stream to text file but the problem that is coming is the object through which i am redirecting is of type None .我遇到了一个问题。我试图将输出流重定向到文本文件,但即将出现的问题是我重定向的对象的类型为None After redirecting to file, the only output i am able to see is None .重定向到文件后,我能看到的唯一输出是None

from contextlib import contextmanager


@contextmanager
def stdout_redirected(new_stdout):
   save_stdout = sys.stdout
   sys.stdout = new_stdout
   try:
       yield None
   finally:
       sys.stdout = save_stdout

    with open('output3.txt', "w") as f:
        with stdout_redirected(f):
            XYZ = hid.core.show_hids(target_vid=vendor, target_pid=product)
            print XYZ

How to redirect the output from none type object ?如何重定向无类型对象的输出?

You can't redirect the output from a None type object.您不能重定向None 类型对象的输出。 Such objects don't have any output.这样的对象没有任何输出。

Nothing you can do in your code is going to make this line您在代码中所做的任何事情都不会使这一行

XYZ = hid.core.show_hids(target_vid=des, target_pid=bes)

work the way you want it to.以您希望的方式工作。 That is because your code is assigning the return value of the function to XYZ and show_hids() returns the value None .那是因为您的代码正在将函数的返回值分配给XYZshow_hids()返回值None You may think that is not how it should work, but that is how it actually does work.你可能认为这不是它应该如何工作,但它实际上是如何工作的。 If a function does return None , or (as in this case) lacks a return statement, then None is what you are going to get back and you can't change that without rewriting the function.如果函数确实return None ,或者(在这种情况下)缺少return语句,那么None就是您要返回的内容,并且您无法在不重写函数的情况下更改它

You can see output, so show_hids() is clearly sending that output to the console itself.您可以看到输出,因此show_hids()显然是将该输出发送到控制台本身。 If you want it to go somewhere else you have to get show_hids() to send the output where you want it.如果你想让它去其他地方,你必须让show_hids()将输出发送到你想要的地方。

You were advised in the comments to consult the documentation on this subject.评论中建议您查阅有关此主题的文档。 Understand that small third-party libraries maintained by only one or two people are likely to have documentation that is less comprehensive than you would like.了解仅由一两个人维护的小型第三方库的文档可能没有您希望的那么全面。 That in turn means it may take a little more work to consult.这反过来意味着可能需要更多的工作来咨询。 In this case, all you needed to do was go to the project page on GitHub and look at the examples folder , which contains sample code called show_hids.py and that contains this sample Python 2 call:在这种情况下,您需要做的就是转到 GitHub 上的项目页面并查看examples 文件夹,其中包含名为show_hids.py示例代码并包含此示例 Python 2 调用:

output = codecs.getwriter('mbcs')(sys.stdout)
hid.core.show_hids(output = output)

This makes it very clear that you are expected to pass an open file object as the parameter output to the function if you want the output to go somewhere other than sys.stdout .这很清楚,如果您希望输出到sys.stdout以外的其他地方,您应该将打开的文件对象作为参数output传递给函数。

But that may only get you so far.但这可能只能让你走到这一步。 Some USB devices have Unicode characters in the device names, and when I run the the sample code under Python 2 it chokes on the name Natural® Ergonomic Keyboard 4000 because of the trademark sign.一些 USB 设备在设备名称中包含 Unicode 字符,当我在 Python 2 下运行示例代码时,由于商标符号,它在名称Natural® Ergonomic Keyboard 4000上窒息。 And fiddling with codecs.getwriter() won't change that because the problem isn't happening at the output stage.摆弄codecs.getwriter()不会改变这一点,因为问题不会发生在输出阶段。

I've posted it as an issue on GitHub but if it were up to me I would flag it Will not fix because it is a pure Python 2 problem, and support for Python 2 ends this year.我已将其作为问题发布在 GitHub 上,但如果由我决定,我会标记它不会修复,因为它是一个纯 Python 2 问题,对 Python 2 的支持将于今年结束。

So if you run into this problem I recommend that you switch to Python 3. Teaching yourself Python 2 is a dead end.因此,如果您遇到这个问题,我建议您切换到 Python 3。自学 Python 2 是一个死胡同。

Then all you need to do is this:那么你需要做的就是:

with open(r"C:\Users\xxxxx\55561448.txt", "w", encoding="UTF-8") as f:
    hid.core.show_hids(output=f)

When I suggested in the comment that you try reassigning sys.stdout as a last resort, this is what I meant:当我在评论中建议您尝试重新分配sys.stdout作为最后的手段时,这就是我的意思:

default_stdout = sys.stdout
sys.stdout = open('help.txt', 'w')
hid.core.show_hids(target_vid=des, target_pid=bes)
sys.stdout = default_stdout

That temporarily monkeypatches sys.stdout so that show_hids() will do what you want.暂时monkeypatches sys.stdout以便show_hids show_hids()执行您想要的操作。 But there is no need to do that in this case because the show_hids() itself provides a means to send the output where you want.但是在这种情况下不需要这样做,因为show_hids()本身提供了一种将输出发送到您想要的位置的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM