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

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

  1. sys.stdinsys.stdin
  2. input() built-in functioninput()内置函数
  3. fileinput.input() functionfileinput.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 sysfor line in sys.stdin:if 'Exit' == line.rstrip():breakprint(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示例

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:breakprint(f'Processing Message from input() *****{data}*****')print("Done")

Output:

输出:

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 fileinputfor fileinput_line in fileinput.input():if 'Exit' == fileinput_line.rstrip():breakprint(f'Processing Message from fileinput.input() *****{fileinput_line}*****')print("Done")

Output:

输出:

Python fileinput.input() Read Standard Input

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

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

如何在Python中从stdin读取相关推荐

  1. python语言arrows用法_如何在Python中使用pyarrow读取parquet文件

    我已经从数据库中创建了一个具有三列(id.author.title)的parquet文件,并希望使用条件(title='learnpython')读取拼花板文件. 下面提到的是我用于这个POC的pyt ...

  2. float在python_如何在python中读取.float文件? - python

    Improve this question 我正在处理大脑MRI数据,它是.float数据. 您知道如何在python中使用它吗? 与 with open('[43x25520].float') as ...

  3. python读取ansi编码文件,如何在Python中同时读取ANSI和Unicode txt文件?

    我是python新手,遇到了一个奇怪的问题: 当一个目录中有50个txt文件时,我希望读取每个.txt文件并将其内容保存在一个唯一的变量中,例如:**file = open(fcf[i], 'r') ...

  4. python读json文件数组_如何在python中从json文件读取json对象数组

    我有一个名为example.json的json文件,包含以下内容[{ "product/productId" : "XXX", "product/ti ...

  5. 如何在 Python 中读取 .data 文件?

    什么是 .data 文件? 创建.data文件是为了存储信息/数据. 此格式的数据通常以逗号分隔值格式或制表符分隔值格式放置. 除此之外,该文件可以是二进制或文本文件格式.在这种情况下,我们将不得不找 ...

  6. python如何读取uni文件_如何在Python中通过HTTP与UniProt交谈?

    我试图从UniProt获得一些结果,这是一个蛋白质数据库(细节并不重要).我正在尝试使用一种从一种ID转换为另一种ID的脚本.我能够在浏览器上手动执行此操作,但无法在 Python中执行此操作. 在h ...

  7. python中用什么函数读取字符串_如何在Python中获得函数名作为字符串?

    在Python中,如何在不调用函数的情况下以字符串的形式获得函数名? 1 2 3 4def my_function(): pass print get_function_name_as_string( ...

  8. 编程中python怎么读-编程语言如何在Python中读写文件

    从文件读取和写入文件是任何编程语言的常见需求.任何文件在读写之前都需要打开.大多数编程语言都使用open()方法来打开文件,以便使用文件对象(file object)读写.可以使用不同类型的文件访问模 ...

  9. python隐藏启动台_如何在Python中启动后台进程?

    如何在Python中启动后台进程? 我正在尝试将shell脚本移植到更易读的python版本. 原始shell脚本在后台使用"&"启动多个进程(实用程序,监视器等). 如何 ...

最新文章

  1. php动态包含文件路径,ThinkPHP实现动态包含文件的方法
  2. 分数化小数(模拟除法操作)
  3. MySQL中的数据分组
  4. MongoDB 小试牛刀
  5. NHibernate 3.0在PetShop 3层架构中的应用 系列
  6. Linux系统任务计划(at、crontab)的使用方法
  7. 内网转外网方法 Sunny-Ngrok
  8. 这一次,彻底弄懂 Java 字节码文件!
  9. 华强北突围:比特币挖矿机成了最赚钱的生意
  10. C语言计算表达式咋写,C语言如何计算表达式(x++)+(++x)+(x++)
  11. mysql 转字符串 blob_BLOB转换为字符串或图像/ PHP或SQL
  12. GET和POST本质区别
  13. 全球及中国坚果产业发展现状及趋势分析,市场发展潜力巨大「图」
  14. 5G千兆无线路由器,国产工业级稳定通信
  15. C语言switch练习之输入某年某月某日,判断这一天是这一年的第几天。
  16. ES5-ES6-ES7_字符串与JOSN格式的数据相互转换以及深度克隆新对象
  17. 使用IDEA如何对Java项目进行打包
  18. Java中的请求域(Request)《笔记》
  19. Nessus 扫描web服务
  20. 修改ttf字体美化显示效果

热门文章

  1. IE10-浏览器实现placeholder效果
  2. [swustoj 1091] 土豪我们做朋友吧
  3. Android ADV 虚拟卡常见错误Failed to push的解决
  4. [转]导出数据到Excel的几种方法
  5. [转载] python difference用法_set.difference() 的用法(python3)_python3 set集合,三元运算以及
  6. [C语言] 插入排序之希尔(shell)排序的特性及实现
  7. 计蒜客---N的-2进制表示
  8. hive-0.11.0安装方法具体解释
  9. 自由缩放属性-resize(禁止textarea的自由缩放尺寸功能)
  10. 20145206《Java程序设计》实验五Java网络编程及安全