python - 我如何从标准输入读取?

标签 python stdin

我如何读取 stdin ?一些code golf挑战需要使用 stdin 进行输入。

最佳答案

使用 fileinput模块:

import fileinput

for line in fileinput.input():
    pass

fileinput将循环遍历指定为命令行参数中给出的文件名的输入中的所有行,如果没有提供参数,则循环遍历标准输入。

注意:line 将包含尾随换行符;删除它使用 line.rstrip() .

关于python - 我如何从标准输入读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21943092/

相关文章:

python - 为什么 PyCharm 在此处使用 @property 时会发出警告?

Python - 运行 Excel 宏

python - 在 Ubuntu 16.04 LTS 中从 Python 3.5 升级到 Python 3.6 后出现的问题

C 不使用 fscanf 从 txt 读取整数

python - 从文件或 STDIN 读取

c# - 将文本数据从文件重定向到 stdin c# windows

python - 在 pisa html 到 pdf 库中使用图像 URL 进行 link_callback

python - 我可以在 VBA 中使用我的 python 自定义对象吗?

Python 如何从 sys.stdin.readline() 中删除换行符

c - 如何使用 VC++ 编译器刷新 .C 程序中的标准输入设备?