Home python Implementation of stdin and stdout

Implementation of stdin and stdout

Author

Date

Category

In the task (except for the solution algorithm itself), it is necessary to implement data reception via stdin , and output via stdout . Before that, I usually implemented the reception through input , and the output through print , or simply returned the value without printing through return .
How to receive data using stdin / stdout is not catching.
Huge request, give a link or example code to understand.


Answer 1, authority 100%

Default input () reads data from stdin , print () prints data to stdout . So you can consider your problem solved.

In Python, stdin , stdout are sys.stdin , sys.stdout objects (text streams , as a rule), which in general can be of any type (if their interface suffices file -like) and can be overridden by anyone (IDLE, bpython, ipython, IDE, win-unicode-console, etc). Sometimes it is sufficient to provide an object that supports a single .write () method if you only want the print () function to support. Otherwise, even an instance of io.TextIOWrapper (the default type sys.stdin / sys.stdout ) may not be sufficient if .fileno () does not return a real file descriptor (see Redirect stdout to a file in Python? ).

When Python starts up, sys.stdin / sys.stdout usually point to standard I / O streams inherited from the parent process or received from the console. Interactive I / O is usually associated with a terminal. From the shell, it is easy to redirect I / O from a file, pipe

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

Working directly with sys.stdin , sys.stdout is the same as with other text files. For example, to read lines of text from stdin and write the entered characters (Unicode codepoint) on each line in reverse order to stdout :

#! / usr / bin / env python3
import sys
for line in sys.stdin:
  print (line.rstrip ('\ n') [:: - 1])

The encoding used by sys.stdin / sys.stdout to convert text to bytes and back may vary depending on the environment. To avoid krakozyab or UnicodeEncodeError exception due to work with arbitrary Unicode characters in the Windows console and other platforms follow the links here that show win-unicode-console (PEP 528 maybe will remove this package ), LC_ * (locale), PYTHONIOENCODING solutions. PEP-538, PEP-540 implemented in Python 3.7 forces Python to use utf-8 in more cases, making problems with I / O encodings much less likely by default.

print () is a handy wrapper around sys.stdout.write () . input () can often be thought of as a wrapper around sys.stdin.readline () for interactive input (maintains input history, editing with readline module, if available). For advanced support for interactive input in the terminal, have a look at prompt_toolkit :

#! / usr / bin / env python
from prompt_toolkit import prompt # $ pip install prompt_toolkit
if __name__ == '__main__':
  answer = prompt ('Give me some input:')
  print ('You said:% s'% answer)

Answer 2, authority 60%

stdin and stdout are file-like objects provided by the OS.

To read and write to them, you need to import sys – import sys .

sys.stdin.read () use to read from stdin

to write to stdout , you can use print (this is how it is used – the most common method of writing to stdout ). those. print writes to sys.stdout .

Example:

import sys
str = sys.stdin.read ()
print str

Answer 3

def countdistinctfivisors (n, start = 2):
  if n == 1:
    return 1
  else:
    result = 0
    for i in range (start, n + 1):
      if n% i == 0:
result + = countdistinctfivisors (n // i, i + 1)
    return result
print (countdistinctfivisors (int (input ())))

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions