如果要打印超过1000行,如何向python添加一些内容以向您发出警告

如果要打印超过1000行,如何向python添加一些内容以向您发出警告,python,printing,Python,Printing,我在用ipython笔记本。我在调试时使用了很多打印语句,但有时我无意中打印了太多,这会使我的计算机冻结。如果您要打印超过10000个字符,有没有办法打开一些东西,告诉python给您一个警告 更新: 谢谢大家的好想法。我会调查日志记录的 如果不编写自己的打印包装函数,就没有简单的方法限制打印输出。但比使用print更好的方法是使用python日志系统进行调试语句,并将输出重定向到文件 import logging logging.basicConfig(filename='example.lo

我在用ipython笔记本。我在调试时使用了很多打印语句,但有时我无意中打印了太多,这会使我的计算机冻结。如果您要打印超过10000个字符,有没有办法打开一些东西,告诉python给您一个警告

更新:
谢谢大家的好想法。我会调查日志记录的

如果不编写自己的打印包装函数,就没有简单的方法限制打印输出。但比使用print更好的方法是使用python日志系统进行调试语句,并将输出重定向到文件

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')

如果不编写自己的打印包装器函数,就无法简单地限制打印输出。但比使用print更好的方法是使用python日志系统进行调试语句,并将输出重定向到文件

import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
而不是

print "hello world"
mprint("hello world",max_length=4)
或者写入文件

with open("asd.txt","w") as f:
    mprint("hello world",max_length=4,out=f)
这也增加了插入数据的能力。。。说明是否要在打印前插入时间戳,或哪一行称为打印

事实上,我在所有日志记录中都使用了类似的函数,但out是全局设置的,而不是随叫随到

虽然使用实际包装的建议很好,但是:

而不是

print "hello world"
mprint("hello world",max_length=4)
或者写入文件

with open("asd.txt","w") as f:
    mprint("hello world",max_length=4,out=f)
这也增加了插入数据的能力。。。说明是否要在打印前插入时间戳,或哪一行称为打印

事实上,我在所有日志记录中都使用了类似的函数,但out是全局设置的,而不是随叫随到


尽管使用实际软件包的建议也很好:

在Python3中,如果您想在模块级替换print函数,可以这样做

_print = print
def print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
    # this test could probably be more sophisticated, to test for
    # any file whose file descriptor is the same as stdout or stderr
    if file in (sys.stdout, sys.stderr):
        output = io.StringIO()
        _print(*objects, sep=sep, end=end, file=output)
        data = output.getvalue()
        if len(data) > 10000:
            # do the "are you sure?" interaction, however you want that to work
        _print(data, end='', file=file, flush=flush)
    else:
        _print(*objects, sep=sep, end=end, file=file, flush=flush)
老实说,你可能不想弄乱内置的东西,即使你可以。但原则是一样的,不管你叫它什么


在Python2中,print不采用相同的选项,而且它是一个关键字,因此您无法截取它,并且即使您希望也无法调用您的函数。因此,您可以编写一个类似于Python3 print的函数,但可以使用Python2 print作为底层机制,也可以使用from\uuuuu future\uuuuuu导入print\u函数。另外,StringIO位于名为StringIO而不是io的模块中。

在Python3中,如果您想在模块级替换print函数,可以这样做

_print = print
def print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False):
    # this test could probably be more sophisticated, to test for
    # any file whose file descriptor is the same as stdout or stderr
    if file in (sys.stdout, sys.stderr):
        output = io.StringIO()
        _print(*objects, sep=sep, end=end, file=output)
        data = output.getvalue()
        if len(data) > 10000:
            # do the "are you sure?" interaction, however you want that to work
        _print(data, end='', file=file, flush=flush)
    else:
        _print(*objects, sep=sep, end=end, file=file, flush=flush)
老实说,你可能不想弄乱内置的东西,即使你可以。但原则是一样的,不管你叫它什么


在Python2中,print不采用相同的选项,而且它是一个关键字,因此您无法截取它,并且即使您希望也无法调用您的函数。因此,您可以编写一个类似于Python3 print的函数,但可以使用Python2 print作为底层机制,也可以使用from\uuuuu future\uuuuuu导入print\u函数。此外,StringIO位于名为StringIO而不是io的模块中。

s=您要打印的内容;如果lensDo你真的想这么做?否则,考虑使用日志记录。特别是向记录器添加适当的处理程序,并最终记录到一个文件中。s=要打印的内容;如果lensDo你真的想这么做?否则,考虑使用日志记录。特别是将适当的处理程序添加到记录器并最终记录到文件中。这可能是@user3314418真正要搜索的内容。我支持OP可能真正想要的内容这可能是@user3314418真正要搜索的内容。我支持OP可能真正想要的内容这一事实主意但恕我直言,这真的对非专家有用吗?@wolfrevo:我不知道。我对在打印之前添加交互的想法持怀疑态度,但我认为如果这是您真正决定要做的,那么无论您的专业水平如何,这段代码都同样有用:-。其他人已经讨论了这个问题,我不是在回答你的实际问题,因为你不应该问这个角度。好主意!但恕我直言,这真的对非专家有用吗?@wolfrevo:我不知道。我对在打印之前添加交互的想法持怀疑态度,但我认为如果这是您真正决定要做的,那么无论您的专业水平如何,这段代码都同样有用:-。其他人已经讨论了这个问题,我没有回答你的实际问题,因为你不应该问这个角度。