输入数字怎么变成大写python_python:输入时将输入转换为大写

How can my Python program convert the user's keyboard input to upper case, as though the user has CAPS LOCK enabled?

I want to emulate a program running on a computer which doesn't have any lower-case letters (i.e. a character set more limited than ASCII).

The text input, typed interactively by the user at the keyboard, should be forced to upper case in real time as though that's what the user typed.

The command line interface could use Python's standard cmd library, or something else. The conversion could be done by a custom input stream, or some other way.

I am not interested in completely re-implementing the character-by-character input system; for example, I would like to continue making use of readline if it is available. I only want to translate the resulting stream as it appears visually and as it comes into the program.

There is a Unix terminal attribute, OLCUC, which sounds promising; but it is marked LEGACY in the Single Unix Specification, and its implementation is unreliable (causes garbage for some normal control sequences or Unicode text). So that is not a solution.

Especially important is that the interface should appear visibly indistinguishable from someone actually typing upper case text; the display should only ever show the user's input as the upper case text the program will process.

解决方案

Append after stdin.read in __call__ func the following:

try:

tty.setraw(sys.stdin.fileno())

ch = sys.stdin.read(1).upper()

sys.stdout.write(ch)

...

You can just do a conditional while (e.g. until user presses enter) to get char by char and echo it as upper case:

getch = _Getch()

text = ''

while True:

char = getch()

if ord(char) == 13: # ascii carriage return

break

else:

text += char

print text

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值