python - 在 Python input() 函数中插入分隔符
问题描述
我正在寻找一种在input()
函数中插入分隔符的方法。
需要明确的是,我的脚本要求用户在 H:MM:SS 中输入超时,我希望 Python 自己插入:
。因此,如果用户键入“14250”,我希望终端显示 1:42:50(在输入期间,而不是在按 ENTER 后)。
在 Python 中可能吗?
解决方案
这是适用于 Linux 的版本。它基于anurag 的版本并且非常相似,但是 Linux 的getch
模块不知道getwch
and putwch
,因此必须替换它们。
from getch import getche as getc
def gettime():
timestr = ''
print('Enter time in 24-hr format (hh:mm:ss): ', end='', flush=True)
for i in range(6):
timestr += getc() # get and echo character
if i in (1, 3): # add ":" after 2nd and 4th digit
print(":", end="", flush=True)
timestr += ':'
print() # complete the line
return timestr
time = gettime()
print("The time is", time)
样本输出:
Enter time in 24-hr format (hh:mm:ss): 12:34:56
The time is 12:34:56
我认为这也适用于 Windows,from msvcrt import getwche as getc
但我无法对此进行测试。
推荐阅读
- flutter - Is it possible to download and execute an uncompiled dart file (Flutter)
- python - Link prediction to predict edges without common neighbours
- c# - 如何在 C# 中从蓝牙设备接收数据
- spring-boot - 战争应用程序中的jsp文件未在docker容器中加载css链接
- php - 无法与 php 中的浮点值进行比较
- android - 导出aar文件时无法包含依赖库
- python - Reduce the dimension of a tensor using max-pooling layer
- javascript - Material-UI:选项卡没有响应内部的数据表
- javascript - 如何检查表格的值是否与下一个值串联其他突出显示单元格
- python - Python AES 加密。每个文件的前 16 个字符,但一个被编码字节替换