gpt4 book ai didi

python - 在 python 中使用 print 时出现不需要的时间戳

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:59 25 4
gpt4 key购买 nike

我正在使用 autobahn[twisted] 来实现一些 WAMP 通信。在订阅某个主题并从中获取提要时,我将其打印出来。当我这样做时,我得到这样的东西:

2016-09-25T21:13:29+0200 (u'USDT_ETH', u'12.94669009', u'12.99998074', u'12.90000334', u'0.00035594', u'18396.86929477', u'1422.19525455', 0, u'13.14200000', u'12.80000000')

我牺牲了太多时间才把它拿出来。是的,我测试了其他要打印的东西,它打印时没有这个时间戳。这是我的代码:

from twisted.internet.defer import inlineCallbacks
from autobahn.twisted.wamp import ApplicationSession, ApplicationRunner

class PushReactor(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
print "subscribed"
yield self.subscribe(self.onTick, u'ticker')

def onTick(self, *args):
print args

if __name__ == '__main__':
runner = ApplicationRunner(u'wss://api.poloniex.com', u'realm1')
runner.run(PushReactor)

如何删除此时间戳?

最佳答案

好吧,sys.stderrsys.stdout 被重定向到扭曲的记录器。

您需要在运行应用程序之前更改日志记录格式。

参见:https://twistedmatrix.com/documents/15.2.1/core/howto/logger.html

如何重现

您可以使用这个简单的应用程序重现您的问题:

from autobahn.twisted.wamp import ApplicationRunner

if __name__ == '__main__':
print("hello1")
runner = ApplicationRunner(u'wss://api.poloniex.com', u'realm1')
print("hello2")
runner.run(None)
print("hello3")

当进程被终止时,您将看到:

hello1
hello2
2016-09-26T14:08:13+0200 Received SIGINT, shutting down.
2016-09-26T14:08:13+0200 Main loop terminated.
2016-09-26T14:08:13+0200 hello3

在应用程序启动期间,stdout(和stderr)被重定向到一个类似文件的对象(属于twisted.logger._io.LoggingFile类) >).

printwrite 的每次调用都会在twister日志消息中发生更改(每行一个)。

重定向是在类twisted.logger._global.LogBeginner中完成的,看一下beginLoggingTo方法。

关于python - 在 python 中使用 print 时出现不需要的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39690816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com