stdin and stdout are file-like objects provided by the OS. In general, when a program is run in an interactive session, stdin is keyboard input and stdout is the user's tty, but the shell can be used to redirect them from normal files or piped output from and input to other programs.
input() is used to prompt the user for typed input. In the case of something like a programming puzzle, it's normally assumed that stdin is redirected from a data file, and when the input format is given it's usually best to use sys.stdin.read() rather than prompting for input with input(). input() is intended for interactive user input, it can display a prompt (on sys.stdout) and use the GNU readline library (if present) to allow line editing, etc.
print() is, indeed, the most common way of writing to stdout. There's no need to do anything special to specify the output stream. print() writes to sys.stdout if no alternate file is given to it as a file= parameter.
stdin and stdout are file-like objects provided by the OS. In general, when a program is run in an interactive session, stdin is keyboard input and stdout is the user's tty, but the shell can be used to redirect them from normal files or piped output from and input to other programs.
input() is used to prompt the user for typed input. In the case of something like a programming puzzle, it's normally assumed that stdin is redirected from a data file, and when the input format is given it's usually best to use sys.stdin.read() rather than prompting for input with input(). input() is intended for interactive user input, it can display a prompt (on sys.stdout) and use the GNU readline library (if present) to allow line editing, etc.
print() is, indeed, the most common way of writing to stdout. There's no need to do anything special to specify the output stream. print() writes to sys.stdout if no alternate file is given to it as a file= parameter.
When you run your Python program, sys.stdin is the file object connected to standard input (STDIN), sys.stdout is the file object for standard output (STDOUT), and sys.stderr is the file object for standard error (STDERR).
Anywhere in the documentation you see references to standard input, standard output, or standard error, it is referring to these file handles. You can access them directly (sys.stdout.write(...), sys.stdin.read() etc.) or use convenience functions that use these streams, like input() and print().
For the Spotify puzzle, the easiest way to read the input would be something like this:
import sys
data = sys.stdin.read()
After these two lines the input for your program is now in the str data.
Standard idiom for reading from stdin, writing to stdout?
Stdin Stdout python - Stack Overflow
python - Stdin and Stdout - Stack Overflow
Confused about stdin and stdout
Videos
I am just a beginner. I just read about the sys module. I am not able to grasp it. Can anyone explain what these two are.
To find the numbers your looking for I would use a positive lookbehind and lookahead function in your regular expression.
(?<=Bond Energy LDA ).*(?= eV)
This checks to see if the thing you are looking at is proceeded by 'Bond Energy LDA' and followed by 'eV' but does not include them in the string you extract. So assuming that the numbers you are looking for are always proceeded and followed by those two things you can find them like that.
A nice way to read from stdin is to use the sys python module.
import sys
Then you can read lines straight from stdin:
import sys
import re
from line in sys.stdin:
matchObj = re.search(r '(?<=Bond Energy LDA ).*(?= eV)', line, re.I)
if(matchObj):
print(matchObj.group())
If the regular expression is not found on the line then matchObj will be null skipping the if statement. If it is found the search will return a matchObj containing groups. You can then print the group to stdout as print will by default print to stdout if no file is given.
Why use a regular expression? Split the input:
>>> s = """<Aug22-2008> <15:37:37> Bond Energy LDA -17.23014168 eV"""
>>> s.split()[5]
'-17.23014168'
Of course, if you can provide more sample input that does not put the number on the 5th position, this perhaps is not enough.
Ask your teacher for more sample input.
STDIN and STDOUT are documented.
stdin and stdout are the streams for your operating system's standard input and output.
You use them to read and write data from your operating system's std input (usually keyboard) and output (your screen, the python console, or such).
print is simply a function which writes to the operting system's stdout and adds a newline to the end.
There are more features in print than just this, but that's the basic idea.
# Simplified print implementation
def print(value, end='\n'):
stdout.write(value)
stdout.write(end)
stdin and stdout are stream representations of the standard in- and output that your OS supplies Python with.
You can do almost everything you can do with a file on these, so for many applications, they are far more useful than eg. print, which adds linebreaks etc.