Maximum characters that can be stuffed into raw_input() in Python

微笑、不失礼 提交于 2019-12-11 01:32:43

问题


For an InterviewStreet challenge, we have to be able to accomodate for a 10,000 character String input from the keyboard, but when I copy/paste a 10k long word into my local testing, it cuts off at a thousand or so.

What's the official limit in Python? And is there a way to change this?

Thanks guys

Here's the challenge by-the-by:

http://www.interviewstreet.com/recruit/challenges/solve/view/4e1491425cf10/4edb8abd7cacd


回答1:


Are you sure of the fact that your 10k long word doesn't contain newlines?


raw_input([prompt])

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

...

If the readline module was loaded, then raw_input() will use it to provide elaborate line editing and history features.

There is no maximum limit (in python) of the buffer returned by raw_input, and as I tested some big length of input to stdin I could not reproduce your result. I tried to search the web for information regarding this but came up with nothing that would help me answer your question.

my tests

  :/tmp% python -c 'print "A"*1000000' | python -c 'print len (raw_input ())';
  1000000
  :/tmp% python -c 'print "A"*210012300' | python -c 'print len (raw_input ())';
  210012300
  :/tmp% python -c 'print "A"*100+"\n"+"B"*100' | python -c 'print len (raw_input ())'; 
  100



回答2:


I guess this is part of the challenges. The faq suggest raw_input() might not be the optimal approach:

The most common (possibly naive) methods are listed below. (...)

There are indeed Python standard modules helping to handle system input/output.




回答3:


I had this same experience, and found python limits the length of input to raw_input if you do not import the readline module. Once I imported the readline module, it lifted the limit (or at least raised it significantly enough to where the text I was using worked just fine). This was on my Mac with Python 2.7.15.



来源:https://stackoverflow.com/questions/8469122/maximum-characters-that-can-be-stuffed-into-raw-input-in-python

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