简体   繁体   English

使用Python中的sys.stdin.readline从stdin读取非常大的行

[英]Reading very large line from stdin using sys.stdin.readline in Python

While trying to write a program to certain specifications, which include requiring the program to read information from stdin and print output to stdout , I have run into an unexpected problem. 在尝试将程序编写为某些规范时,包括要求程序从stdin读取信息并将输出打印到stdout ,我遇到了一个意想不到的问题。 The problem is that sys.stdin.readline() doesn't finish reading the line provided to it on stdin . 问题是sys.stdin.readline()没有完成读取stdin上提供给它的行。

Minimal Example 最小的例子

#!/bin/python

from sys import stdin
myline = stdin.readline().split()

The line to be read is found at http://pastebin.com/k3DsD5f6 要阅读的行可在http://pastebin.com/k3DsD5f6上找到

On my MacBook Pro running 10.8.5 and Python 2.7, the program hangs indefinitely at the line involving the readline() command. 在运行10.8.5和Python 2.7的MacBook Pro上,程序无限期地挂起涉及readline()命令的行。

The exact terminal output I get (assuming I name this snippet of code so_error.py ) is as follows. 我得到的确切终端输出(假设我将这段代码so_error.pyso_error.py )如下所示。

python so_error.py
30887 92778 36916 47794 38336 85387 60493 16650 41422 2363 90028 68691 20060 97764 13927 80541 83427 89173 55737 5212 95369 2568 56430 65783 21531 22863 65124 74068 3136 13930 79803 34023 23059 33070 98168 61394 18457 75012 78043 76230 77374 84422 44920 13785 98538 75199 94325 98316 64371 66414 3527 76092 68981 59957 41874 6863 99171 6997 97282 2306 20926 77085 36328 60337 26506 50847 21730 61314 25858 16125 53896 19583 546 98815 33368 15435 90365 44044 13751 71088 26809 17277 47179 95789 93585 5404 2652 92755 12400 99933 95061 49677 93369 47740 10013 36227 98587 48095 97540 40796 80571 51435 60379 97468 66602 10098 12903 73318 70493 26653 60757 97302 60281 24287 9442 53866 29690 28445 46620 58441 44730 58032 8118 38098 5772 34482 90676 20710 98928 4568 77857 79498 72354 54587 76966 55307 64684 6220 28625 51529 32872 5733 48830 9504 30020 58271 63369 59709 86716 26341 18150 47797 724 42619 2246 22847 93452 92922 43556 92380 97489 37765 88229 69842 92351 65194 41501 57035 87765 70125 24915 36988 75857 73744 46

It always stops at this exact point, so this is likely some kind of an overflow error. 它总是停在这个确切的位置,所以这可能是某种溢出错误。 The code works just fine with a short line. 代码适用于短线。

Your problem is not readline() at all but the way you execute the script. 除了你执行脚本的方式之外,你的问题根本不是readline() Since you're reading from stdin you need to supply stdin with the data to be read. 由于您正在从stdin读取,因此需要为stdin提供要读取的数据。 If you don't, the program will hang indefinitely (same as if you ran cat with no arguments). 如果不这样做,程序将无限期挂起(就像你运行没有参数的cat )。

I took your example code and input file and was able to successfully run it like this: 我拿了你的示例代码和输入文件,并能够像这样成功运行它:

$ python so_error.py <longline.txt

It exits successfully and rather quickly. 它成功而且很快就退出了。 I also added print myline at the end of your script to verify that it was indeed parsing everything correctly. 我还在脚本的末尾添加了print myline ,以验证它确实正确地解析了所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM