аватар question@mail.ru · 01.01.1970 03:00

STDIN and STDOUT implementation

In the problem (except for the solution of the solution itself), it is necessary to implement data receipt through stdin , and the output through stdout . Prior to this, I usually realized a reception through input , and the conclusion through print or simply retued the value without printing through retu . How to accept the data using stdin / stdout I do not catch. A huge request, give a link or example of code to understand.

аватар question@mail.ru · 01.01.1970 03:00

by default, reads data from, prints data in. So you can consider your task to be solved.

in the python stdin , stdout are represented by objects (, as a rule), which in the general case can be of any type (if their interface is sufficiently File-Like) and can be resettled) by someone (IDLE, BPYTHON, IPYTHON, IDE, WIN-Unicode-CONSOLE, ETC). Sometimes it is enough to provide an object that supports the only .Write () method, if you only need print () to maintain the function. In other cases, even an instance (type sys.stdin / sys.stout by default) may be insufficient if the real File Descriptor does not retu (see details B).

when starting python sys.stdin / sys.studout usually indicate standard input/output flows inherited from the parental process or received from the console. Interactive input/conclusion is usually associated with the terminal. It is easy to redirect the input/output from the file from the file, channel (pipe)

  $ python vas-program.py & lt; input-file ######## `sys .__ stdin__` is the input-file   $ echo abc | Python Your Program.py # `sys .__ stdin__` This is pipe (` echo` writes from one end, we read from the other)      

work directly with sys.stin sys.stdout the same as with other text files. For example, to read text lines from standard input and record the introduced symbols (unicode codepoint) in each line in the reverse order in standard conclusion

  #!/USR/Bin/Env Python3    Import  sys  for  line  in  sys.stdin:   print  (line.rstrip ( '\ n' ) [::- 1 ])     

the encoding used sys.stdin / sys.studut so that the text into bytes is tued back and vice versa, it may depend on the environment. To avoid unicodeencodeeroror exceptions because of and follow the links given here, which show win-toicode-heart (), lc _* (locale), pythonioencoding solution. They force Python to use UTF-8 in more cases, making problems with input/output encoding are much less likely by default.

print () This is a convenient Wrapper around SYS.STDOUT.WRITE () . Input () can often be considered as a Wrapper around sys.stdin.readline () , designed for interactive input (support for the history of the input, editing with a module, if available). For advanced support for interactive input in the terminal, look at:

  #!/USr/Bin/Env Python    from  prompt_toolkit  import  prompt # $ pip Install Prompt_Toolkit    if  __name__ ==  '' __ main __ ':  Anter = Prompt (' give me some input: ')   Print  ( 'you said: % s'                                    

Latest

Similar