简体   繁体   English

在 Python 3 中使用 Ctrl-D 和 sys.stdin.readlines() 后,如何避免 input() 出现 EOFError?

[英]How can I avoid an EOFError for input() after using Ctrl-D with sys.stdin.readlines() in Python 3?

I am a total newbie trying to teach myself Python for personal growth and development.我是一个完全新手,试图自学 Python 以促进个人成长和发展。 So please go easy on me.所以请go对我放心。 (If ever have any biology questions, I'll be happy to return the favor!) (如果有任何生物学问题,我很乐意回报您!)

I am trying to write a program in PyCharm CE on MacOSX (10.14.2 Mojave) to do the following:我正在尝试在 MacOSX(10.14.2 Mojave)上的 PyCharm CE 中编写一个程序来执行以下操作:

1) let the user enter a chunk of text with multiple lines at once by copying/pasting from a source. 1)让用户通过从源复制/粘贴一次输入一段多行的文本。 For instance:例如:

Mary and Beth玛丽和贝丝

went to去了

the park.公园。

2) Join all the lines into one, replacing the \n for spaces, as follows: 2) 将所有行合二为一,将 \n 替换为空格,如下所示:

Mary and Beth went to the park.玛丽和贝丝去了公园。

I've been doing a lot of reading around, and I've found that a preferred way to have users enter a multi-line piece of text at once is using sys.stdin.readlines(), making sure that the user calls an End Of File with Control-D.我已经做了很多阅读,我发现让用户一次输入多行文本的首选方法是使用 sys.stdin.readlines(),确保用户调用使用 Control-D 结束文件。 So far I've come up with the following到目前为止,我已经想出了以下内容

import sys


print('''What is the text that you would like to enter?
         (press command-d at the end)\n''')

orig_text = sys.stdin.readlines()
one_string = "".join(orig_text)
one_string = one_string.replace('\n','')
print(one_string)

So far, so good - one_string prints "Mary and Beth went to the park."到目前为止,一切都很好 - one_string 打印“玛丽和贝丝去了公园”。

The problem is further down the code, when I use a regular input() function...当我使用常规 input() function ...

search_word = input('Which word would you like to replace?')
print(search_word)

I get the following error message: EOFError: EOF when reading a line我收到以下错误消息: EOFError: EOF when reading a line

I've seen posts from others with a similar problem, and some answers suggest that I try...我看过其他有类似问题的帖子,有些答案建议我尝试...

sys.stdin.close()
sys.stdin = open('/dev/tty')
search_word = input('Which word would you like to replace?')
print(search_word)

I tried that, but now I get the following error: OSError: [Errno 6] Device not configured: '/dev/tty' .我试过了,但现在我收到以下错误: OSError: [Errno 6] Device not configured: '/dev/tty' I also tried sys.stdin.flush() , to no avail.我也试过sys.stdin.flush() ,但无济于事。

At this point, I gave up and decided to ask the professionals: a) Are there better approaches to having a user copy and paste multi-line text into the program;在这一点上,我放弃了,决定问专业人士: a) 有没有更好的方法让用户将多行文本复制并粘贴到程序中; b) If my approach so far is OK, how can I get rid of the OSError without breaking my computer? b)如果到目前为止我的方法还可以,我怎样才能在不破坏计算机的情况下摆脱 OSError?

Thanks a ton in advance!提前致谢! Mariano马里亚诺

sys.stdin.readline() isn't a good solution. sys.stdin.readline() 不是一个好的解决方案。

You could use the fileinput module:您可以使用fileinput模块:

import fileinput

for line in fileinput.input():
    ... code ...

fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided. fileinput将遍历输入中指定为命令行 arguments 中给出的文件名的所有行,如果没有提供 arguments,则循环遍历标准输入。

Your code could be substituted by您的代码可以替换为

one_string = "".join(map(str.rstrip, fileinput.input()))

rstrip removes trailing line feeds and spaces. rstrip删除尾随的换行符和空格。

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

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