python - 粘贴到Python时如何截断回车
问题描述
我创建了一个将复制到系统剪贴板的函数。但是,当我从剪贴板粘贴值时,它会自动执行回车。这极大地影响了我程序中的计算。
注意:不能使用 Pyperclip 或任何其他安装。我只能为此使用 Python IDLE 3.8 中包含的内容
我尝试使用带有 clipboard_answer 变量的 strip() 方法。它仍然返回到下一行
def copy(solution_answer):
clipboard_answer = str(solution_answer)
command = 'echo ' + clipboard_answer.strip() + '| clip' # Creates command variable, then passes it to the os.system function as an argument. CMD opens and applys echo (number calculated) | clip and runs the clipboard function
os.system(command)
print("\n\n\n\n",solution_answer, "has been copied to your clipboard") # Used only for confirmation to ensure copy function runs
假装“|” 图标是光标
我有一个复制到我的剪贴板的解决方案,即 25
当我在程序中按 CTRL+V 时,我希望它会这样做
25 |
但实际上光标是这样的
25
|
解决方案
不要使用os.system
. 使用subprocess
,您可以将字符串直接提供给标准输入,clip
而无需调用 shell 管道。
from subprocess import Popen, PIPE
Popen(["clip"], stdin=PIPE).communicate(bytes(solution_answer))
推荐阅读
- javascript - 多标签页上的图像模式 - 已解决
- azure-cosmosdb - 有没有办法使用 CosmosDb 的 TTL 功能但始终保持至少 n 条记录
- angular - 如何在primeng中禁用NodeTree(父母和孩子)?
- powershell - 如何使用powershell在txt文件中按日和月过滤
- django - DJ_REST_AUTH 错误:NameError:未定义名称“dj_rest_auth”
- api - 执行 GET 请求时出现 ECONNRESET 错误
- ios - 点击预览时如何扩展推送通知(IOS)?
- angular - 我在一个页面上有两个表单,但提交按钮对两个验证都有效?
- flutter - 如何显示firebase存储文件上传进度
- angular - 如何像在 Windows 文件资源管理器中那样在 Angular 中实现面包屑?