- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有以下 Python 脚本,它读取数字并在输入不是数字时输出错误。
import fileinput
import sys
for line in (txt.strip() for txt in fileinput.input()):
if not line.isdigit():
sys.stderr.write("ERROR: not a number: %s\n" % line)
如果我从 stdin 获得输入,我必须按 Ctrl + D 两次 来结束程序。为什么?
当我单独运行 Python 解释器时,我只需按一次 Ctrl + D。
bash $ python test.py
1
2
foo
4
5
<Ctrl+D>
ERROR: not a number: foo
<Ctrl+D>
bash $
最佳答案
在 Python 3 中,这是由于 a bug in Python's standard I/O library .该错误已在 Python 3.3 中修复。
在 Unix 终端中,键入 Ctrl+D 实际上并不会关闭进程的标准输入。但是键入 Enter 或 Ctrl+D 确实会导致 OS read
系统调用立即返回。所以:
>>> sys.stdin.read(100)
xyzzy (I press Enter here)
(I press Ctrl+D once)
'xyzzy\n'
>>>
sys.stdin.read(100)
被委托(delegate)给 sys.stdin.buffer.read
,它在循环中调用系统 read() 直到它累积请求的全部数据量;或者系统 read() 返回 0 字节;或发生错误。 (docs) (source)
在第一行后按 Enter 导致系统 read() 返回 6 个字节。 sys.stdin.buffer.read
再次调用 read() 以尝试获取更多输入。然后我按下 Ctrl+D,导致 read() 返回 0 个字节。此时,sys.stdin.buffer.read
放弃并仅返回它之前收集的 6 个字节。
请注意,该进程仍然在 stdin 上有我的终端,我仍然可以输入内容。
>>> sys.stdin.read() (note I can still type stuff to python)
xyzzy (I press Enter)
(Press Ctrl+D again)
'xyzzy\n'
好的。这是最初提出此问题时被破坏的部分。现在可以了。但在 Python 3.3 之前,存在一个错误。
这个错误有点复杂——基本上问题是两个不同的层在做同样的工作。 BufferedReader.read()
被编写为重复调用 self.raw.read()
直到它返回 0 个字节。然而,原始方法 FileIO.read()
执行了它自己的循环直到零字节。所以当你第一次在有这个错误的 Python 中按下 Ctrl+D 时,它会导致 FileIO.read()
返回 6 个字节给 BufferedReader.read()
,这然后会立即再次调用 self.raw.read()
。第二个 Ctrl+D 会导致 that 返回 0 字节,然后 BufferedReader.read()
最终退出。
不幸的是,这个解释比我之前的解释要长得多,但它的优点是正确。 bug 就是这样......
关于python - 为什么我必须按两次 Ctrl+D 才能关闭标准输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2162914/
我需要触发 Ctrl+R, Ctrl+A, Ctrl+Q 用户点击按钮时的事件。 我正在编写以下代码: $(document).ready(function () { $('#Button1'
我试图让用户只输入数字并将控件复制并粘贴到文本框。我可以限制用户仅输入数字,但复制、粘贴对我不起作用,请帮助我解决此问题。 这是我的脚本: $(".allow_only_numbers").keydo
如何重新映射快速上下命令? 我想将 Ctrl-U 重新映射到 Ctrl-K 并将 Ctrl-D 重新映射到 Ctrl-J? 我尝试了以下但没有成功...... nnoremap nnoremap
我尝试将一些 Eclipse 快捷方式重新绑定(bind)到 Ctrl+M、Ctrl+U、Ctrl+J 或 Ctrl+[ 失败。原来的快捷方式有效,但如果我尝试重新绑定(bind)到列表中的快捷方式,
这是来自谷歌的面试题。我无法自己解决。有人可以解释一下吗? 编写一个程序来打印击键序列,使其生成最大数量的字符“A”。您只能使用 4 个键:A、Ctrl+A、Ctrl+C 和 Ctrl+V。只允许 N
这是来自谷歌的面试题。我无法自己解决。有人可以解释一下吗? 编写一个程序来打印击键序列,使其生成最大数量的字符“A”。您只能使用 4 个键:A、Ctrl+A、Ctrl+C 和 Ctrl+V。只允许 N
我在 C# 中开发了 vsto 模板,在其中,我为 excel 表中的许多字段添加了数据验证。如果用户复制粘贴内容,这些验证规则将被覆盖并且不起作用。 我如何限制用户复制、粘贴、剪切。**有没有其他方
我还是 AngularJS 的新手,所以这里有一个我必须解决的问题: 我制作了一个自定义提交指令,仅当表单有效时才提交表单。可以这样使用: 一切正常,直到我尝试 RegisterCtrl用regis
在网上搜索了一下后,似乎我无法将 CtrlSpace 映射到任何/很多东西。今天有办法做到吗,我发现的通常是2岁的。 最佳答案 我遇到了同样的问题,简短的回答是可以,而且不仅仅是在 GUI 版本中。在
如何简单地想在文本框中输入一个值,使用“Ctrl+a”从文本框中选择完整的文本,然后使用Ctrl + c"然后使用 Selenium + C# 将其粘贴到与 "Ctrl + v"相同的框中。 最佳答案
我有一个模拟ctr+c & ctr+v的小程序(复制粘贴)使用系统 keybd_event 的事件。问题是程序运行后计算机继续运行,就好像按下 ctrl 键然后 - 如果我键入 a 它会选择整个文档,
我正在尝试将 Vim 命令映射到 ctrl+pgup和 ctrl+pgdn组合键。这些键的 vim 语法不起作用(即 和 ,或 和 )。 由于默认的 vim 语法不起作用,我猜测终端没有发送
如何让集成的 VSCode 终端不捕获 CTRL-E、CTRL-X 和 CTRL-A? 实际行为:执行键盘快捷键通常在终端之外执行的操作 预期行为: CTRL-X-E 应该在集成终端中打开 $EDIT
我有一个较旧的基于 Java swing 的独立应用程序,它使用 JFrame 和 JMenuBar,其中包含多个 Jmenu 元素(具有相应的 JMenuItem 项)。 在 Windows(7 和
我已经使用 VsVim 大约一年了。我总是能够使用默认的 ctrl+c 和 ctrl+v 快捷方式复制/粘贴文本。几天前它停止为我工作。我认为 VsVim 键盘设置必须以某种方式改变,以处理这些键而不
在OS X上,我已将“键盘修改器”设置中的Caps-Lock更改为CTRL。很棒,但是我想在 VIM 中使用CTRLCTRL(快速双击)来ESC(我的小指在抵制CTRL [)。可能的? 两秒钟后已经使
是否可以指定在 VIM 中按 CtrlLeft/CtrlRight 键后光标将停止的符号?是否有可能使它的行为像在 Windows 中一样?使用这些组合时光标跳得太远。此外,b/e 键有点帮助,但它们
Google Chrome 具有用于减小字体大小的键绑定(bind) Ctrl-- 和用于减小字体大小的键绑定(bind) Ctrl-+增加字体大小。我想阻止他们。我发现它们必然是 ,我做到了: do
当我更改页面比例(ctrl-+、ctrl--)时,会破坏页面布局。 最后一个元素正在掉落。 帮助我,我需要将布局更改为适当缩放的菜单。 页面: 截图: 最佳答案 因为这对您来说真的很重要,所以这里有一
当我按下 CTRL + + 或 CTRL + - 时,我想将字体大小保持为这是。它不应该变大或变小。如何实现? 最佳答案 似乎没有任何纯 css 解决方案。 并且没有干净的方法来获取当前的缩放级别。
我是一名优秀的程序员,十分优秀!