English 中文(简体)
如何在沙尔展示被改头 the的泥 st?
原标题:How to display the redirected stdin in Python?

I m 开始于“灰色”项目,在该项目中,必须采用类似于以下的代码:

import sys
import StringIO

s = StringIO.StringIO("Hello")
sys.stdin = s
a = raw_input("Type something: ")
sys.stdin = sys.__stdin__
print("You typed in: "+a)

The problem is, after the code runs, the following is displayed:

类型: 页: 1

是否有办法修改我的准则,这样,人们就会看到:

Type something: Hello

页: 1

I ve been searching high and low but have found no answer yet. I ll really appreciate if anyone has an idea. Thanks!

最佳回答

I m not sure why you would need to, but you could always do this:

a = raw_input("Type something: ")
if sys.stdin is not sys.__stdin__:
    print(a)
print("You typed in: "+a)

Then again, swapping raw_input for your own implementation as needed would probably make more sense.

Edit: okay,根据你的意见,认为你想做一些mon子的派遣。 与此类似:

old_raw_input = raw_input

def new_raw_input(prompt):
    result = old_raw_input(prompt)
    if sys.stdin is not sys.__stdin__:
        print result
    return result

raw_input = new_raw_input

当然,这可能会使 st改方向。

问题回答

这样做。

class MyRawInputFakeOutObject( object ):
    def __init__( self, the_fake_out_input_text ):
        self.input= the_fake_out_input_text
    def __call__( self, prompt ):
        print( prompt )
        return self.input

raw_input= MyRawInputFakeOutObject( "Hello" )

import some_existing_module

some_existing_module.the_existing_main()

Now the existing module is working with your raw_input, not the built-in raw_input. Yours can do anything to provide fake inputs and fake outputs.

http://www.un.org。 我认为,在阅读其他答复和评论之后,我找到了真正调整方向的良好办法。 请注意,我假定,你们会知道对终端用户的原始投入。

www.un.org/Depts/DGACM/index_spanish.htm 用户法典(Named some_module.py)

print "running some module with 5 raw_input requests"
for x in range(5):
    value = raw_input("This is someone else s code asking its (" + str(x) + ") raw_input: ")
    print  stdin value:   + value

Your Test /strong> (以你喜欢的东西计)

    import sys
    class MY_STD_IN( object ):
        def __init__(self, response_list):
            self.std_in_list = response_list
            self.std_in_length = len(response_list)
            self.index = 0

        def readline(self):
            value = self.std_in_list[self.index]      
            print value
            if self.index < self.std_in_length -1:
                self.index += 1
            else:
                self.index = 0

            return value

    predetermined_stdin_responses = [ Value 1
 ,  Value 2
 ,  Value 3
 ]
    sys.stdin = MY_STD_IN( predetermined_stdin_responses )

    import some_module

www.un.org/Depts/DGACM/index_spanish.htm 浏览Yelds

running some module with 5 raw_input requests
This is someone else s code asking its (0) raw_input: Value 1
stdin value: Value 1
This is someone else s code asking its (1) raw_input: Value 2
stdin value: Value 2
This is someone else s code asking its (2) raw_input: Value 3
stdin value: Value 3
This is someone else s code asking its (3) raw_input: Value 1
stdin value: Value 1
This is someone else s code asking its (4) raw_input: Value 2
stdin value: Value 2

www.un.org/spanish/ecosoc

不是确定你是否重新寻找这种字面答案,而是在这里

import sys
import StringIO

s = StringIO.StringIO("Hello")
sys.stdin = s
a = raw_input("Type something: ")
sys.stdin = sys.__stdin__
print(a+"
You typed in: "+a)

Yields:

类型: Hello

You typed in: Hello





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签