input()与sys.stdin.read() [英] input() vs sys.stdin.read()

查看:66
本文介绍了input()与sys.stdin.read()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import sys
s1 = input()
s2 = sys.stdin.read(1)

#type "s" for example

s1 == "s" #False
s2 == "s" #True

为什么?如何使 input()正常工作?我尝试对 s1 进行编码/解码,但这不起作用.

Why? How can I make input() to work properly? I tried to encode/decode s1, but it doesn't work.

谢谢.

推荐答案

如果您使用的是Windows,则在键入's'时会注意到 input()的结果输入的是"s \ r" .从结果中删除所有结尾的空格,就可以了.

If you're on Windows, you'll notice that the result of input() when you type an 's' and Enter is "s\r". Strip all trailing whitespace from the result and you'll be fine.

这篇关于input()与sys.stdin.read()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆