How to read content from standard input stdin in PYTHON

anonymity
Release: 2019-05-25 10:35:44
Original
6248 people have browsed it

Python has several ways to read data from standard input.

How to read content from standard input stdin in PYTHON

1. sys.stdin

sys.stdin provides read() and readline() functions. If you want to read line by line, you can Consider using it:

import sys
line = sys.stdin.readline()
while line:
    print line,
    line = sys.stdin.readline()
Copy after login

Note: If there is no data, io will be blocked, so you can do data checking on the standard input (Linux):

import sys
import select
if select.select([sys.stdin], [], [], 0.0)[0]:
    help_file_fragment = sys.stdin.read()
else:
    print("No data", file=sys.stderr)
    sys.exit(2)
Copy after login

2, input()

If you want to use the interactive method (prompt for input), Python 3.x can use input(), while Python 2.x uses raw_input():

x = raw_input('请输入您的名字:')
Copy after login

The above is the detailed content of How to read content from standard input stdin in PYTHON. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!