Does the print function in python have a limit of string length it can print?

送分小仙女□ 提交于 2020-06-27 07:42:50

问题


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.


回答1:


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.

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 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.




回答2:


About the maximum size of your string which you can print in stdout using print function, since you are have to pass your text as a python object to print function and since the max size of your variable is depend on your platform it could be 231 - 1 on a 32-bit platform and 263 - 1 on a 64-bit platform.

Also you can use sys.maxsize to get the max size of your variables :

sys.maxsize

An integer giving the maximum value a variable of type Py_ssize_t can take. It’s usually 2**31 - 1 on a 32-bit platform and 2**63 - 1 on a 64-bit platform.



来源:https://stackoverflow.com/questions/31400077/does-the-print-function-in-python-have-a-limit-of-string-length-it-can-print

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!