简体   繁体   English

sys.stdin.readline() 在 Sublime text3 中从 python 中的 cmd 读取多行

[英]sys.stdin.readline() to read multiple lines from cmd in python in Sublime text3

I am really new on python and try to find a good IDE My friend suggest me sublime text3.我对 python 真的很陌生,并试图找到一个好的 IDE 我的朋友建议我使用 sublime text3。

But when I try to write some code, it come up a question:但是当我尝试编写一些代码时,出现了一个问题:

I try to write a code that will read the line from user input.我尝试编写一个代码来从用户输入中读取该行。

import sys    
a = sys.stdin.readline()    
print (a)

At beginning I expect some cmd jump out and let me type something to let my program read.一开始我希望一些 cmd 跳出来让我输入一些东西让我的程序读取。 But there's nothing happen....但是什么都没有发生......

Can someone tell me that can SublimeText read user input?有人可以告诉我 SublimeText 可以读取用户输入吗? or did I do something wrong....还是我做错了什么......

(I find out that there is a post discuss about 'Using sys.stdin.readline() to read multiple lines from cmd in Python' (我发现有一篇关于“使用 sys.stdin.readline() 在 Python 中从 cmd 读取多行”的帖子讨论

Using sys.stdin.readline() to read multiple lines from cmd in Python 使用 sys.stdin.readline() 从 Python 中的 cmd 读取多行

but I'm not sure if this is the case for sublime.....)但我不确定 sublime 是否是这种情况.....)

Hi everyone again.大家好。 Sorry for the miss leading title and thank you for answering my question again!对不起,领导标题小姐,感谢您再次回答我的问题!

DYZ point out the question I would want to ask, and actually I also try package "SublimeREPL". DYZ 指出了我想问的问题,实际上我也尝试了包“SublimeREPL”。 However, it's not working :(( (the figure show it)但是,它不起作用:(((图中显示)

I also try my code on terminal and it works... Can some one tell me where did I did wrong?我也在终端上尝试我的代码并且它有效......有人可以告诉我我做错了什么吗? or maybe I shouldn't do this on Sublime..或者我不应该在 Sublime 上这样做..

thank you guys again and sorry for confusing!再次感谢你们,很抱歉造成混乱!

enter image description here在此处输入图片说明

Correct me if I misunderstand what you are trying to do, but to get user input in python theres no need for importing sys, just use raw_input().如果我误解了您要执行的操作,请纠正我,但是要在 python 中获取用户输入,不需要导入 sys,只需使用 raw_input()。 For example:例如:

x = raw_input("enter something: ")
print "you entered", x

This program (written in sublime by the way) when run in the command prompt, produced the following output:这个程序(顺便说一下是用 sublime 编写的)在命令提示符下运行时,产生了以下输出:

python user-input.py 
enter something: hello
you entered hello

In this case I entered "hello".在这种情况下,我输入了“你好”。 If you don't need a prompt, just don't pass any parameter to the raw_input method.如果不需要提示,就不要向 raw_input 方法传递任何参数。 Let me know if this wasn't what you needed or if you need any clarification.如果这不是您需要的,或者您需要任何说明,请告诉我。

See this question on using stdin : How to finish sys.stdin.readlines() input?请参阅有关使用stdin问题: 如何完成 sys.stdin.readlines() 输入?

Example below (From the Q):下面的例子(来自Q):

>>> import sys
>>> message = sys.stdin.readlines()
Hello
World
My
Name
Is
James
Bond
# <ctrl-d> EOF sent
>>> print message
['Hello\n', 'World\n', 'My\n', 'Name\n', 'Is\n', 'James\n', 'Bond\n']

If using Python 2.x it is recommended you use the raw_input() function and the input() function in Python 3.x.如果使用 Python 2.x,建议您使用 Python 3.x 中的raw_input()函数和input()函数。 Like below像下面

>>> raw_input()
test
'test'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM