python中的打印函数是否有可以打印的字符串长度限制? [英] Does the print function in python have a limit of string length it can print?

查看:135
本文介绍了python中的打印函数是否有可以打印的字符串长度限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印一个大字符串,它的大小为 100 Mb,需要一次性完成.看起来它被截断了.

I am trying to print a large string, and it is in the magnitudes of a 100 Mb, and it needs to be done in one shot. Looks like it is getting truncated.

推荐答案

虽然这不能回答您的问题,但对于移动大量数据 print 可能是个坏主意:print 意味着用于简短的信息打印输出.它提供了移动大数据时通常不需要的功能,例如格式化和附加 EOL.

while this doesn't answer your question, for moving loads of data print is probably a bad idea: print is meant for short informational printouts. it provides features you usually don't want when moving large data, like formatting and appending an EOL.

相反,在 sys.stdout 文件句柄(或其他一些文件句柄;这允许您轻松写入文件而不是 stdout)上使用更底层的东西,例如 write)

Instead use something more low-level like write on the sys.stdout filehandle (or some other filehandle; this allows you to easily write to a file instead of stdout)

 out=sys.stdout
 out.write(largedata)

您可能还想在输出之前重新分块数据:

you also probably want to re-chunk the data before outputting:

 # this is just pseudocode:
 for chunk in largedata:
     out.write(chunk)

.write 不附加 EOL 字符,因此多个块的输出实际上与一次输出所有块几乎没有区别.但您的好处是不会超出任何缓冲区.

.write does not append an EOL character, so the output of multiple chunks will be virtually indistinguishable from outputting all in one big go. but you have the benefit of not overrunning any buffers.

这篇关于python中的打印函数是否有可以打印的字符串长度限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆