如何在Python中从stdin读取

There are three ways to read data from stdin in Python.

在Python中,有三种方法可以从stdin读取数据。

  1. sys.stdin

    sys.stdin
  2. input() built-in function

    input()内置函数
  3. fileinput.input() function

    fileinput.input()函数

1.使用sys.stdin读取标准输入 (1. Using sys.stdin to read from standard input)

Python sys module stdin is used by the interpreter for standard input. Internally, it calls the input() function. The input string is appended with a newline character (\n) in the end. So, you can use the rstrip() function to remove it.

解释器将Python sys模块 stdin用于标准输入。 在内部,它调用input()函数。 输入字符串的末尾附加换行符(\ n)。 因此,您可以使用rstrip()函数将其删除。

Here is a simple program to read user messages from the standard input and process it. The program will terminate when the user enters “Exit” message.

这是一个简单的程序,可以从标准输入中读取用户消息并进行处理。 当用户输入“退出”消息时,程序将终止。

import sys

for line in sys.stdin:
    if 'Exit' == line.rstrip():
        break
    print(f'Processing Message from sys.stdin *****{line}*****')
print("Done")

Output:

输出:

Hi
Processing Message from sys.stdin *****Hi
*****
Hello
Processing Message from sys.stdin *****Hello
*****
Exit
Done
Python Stdin Example

Python stdin Example

Python stdin示例

Notice the use of rstrip() to remove the trailing newline character so that we can check if the user has entered “Exit” message or not.

请注意,使用rstrip()删除尾随的换行符,以便我们可以检查用户是否输入了“退出”消息。

2.使用input()函数读取标准输入数据 (2. Using input() function to read stdin data)

We can also use Python input() function to read the standard input data. We can also prompt a message to the user.

我们还可以使用Python input()函数读取标准输入数据。 我们还可以向用户提示消息。

Here is a simple example to read and process the standard input message in the infinite loop, unless the user enters the Exit message.

这是一个简单的示例,用于在无限循环中读取和处理标准输入消息,除非用户输入退出消息。

while True:
    data = input("Please enter the message:\n")
    if 'Exit' == data:
        break
    print(f'Processing Message from input() *****{data}*****')

print("Done")

Output:

输出:

Python Input Read From Stdin

Python input() Read From stdin

Python input()从标准输入读取

The input() function doesn’t append newline character to the user message.

input()函数不会在用户消息后附加换行符。

3.使用文件输入模块读取标准输入 (3. Reading Standard Input using fileinput module)

We can also use fileinput.input() function to read from the standard input. The fileinput module provides utility functions to loop over standard input or a list of files. When we don’t provide any argument to the input() function, it reads arguments from the standard input.

我们还可以使用fileinput.input()函数来读取标准输入。 fileinput模块提供实用程序功能来循环标准输入或文件列表。 当我们不向input()函数提供任何参数时,它将从标准输入中读取参数。

This function works in the same way as sys.stdin and adds a newline character to the end of the user-entered data.

该函数的工作方式与sys.stdin相同,并在用户输入数据的末尾添加了换行符。

import fileinput

for fileinput_line in fileinput.input():
    if 'Exit' == fileinput_line.rstrip():
        break
    print(f'Processing Message from fileinput.input() *****{fileinput_line}*****')

print("Done")

Output:

输出:

Python Fileinput Read Standard Input

Python fileinput.input() Read Standard Input

Python fileinput.input()读取标准输入

翻译自: https://www.journaldev.com/32137/read-stdin-python

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值