10.24 2009-09-20 05:48 采纳率: 0%
浏览 1394

如何在 Python 中从 stdin 中读取数据?

I'm trying to do some of the code golf challenges, but they all require the input to be taken from stdin. How do I get that in Python?

转载于:https://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python

  • 写回答

19条回答 默认 最新

  • ℙℕℤℝ 2009-09-20 05:53
    关注

    There's a few ways to do it.

    • sys.stdin is a file-like object on which you can call functions read or readlines if you want to read everything or you want to read everything and split it by newline automatically. (You need to import sys for this to work.)

    • If you want to prompt the user for input, you can use raw_input in Python 2.X, and just input in Python 3.

    • If you actually just want to read command-line options, you can access them via the sys.argv list.

    You will probably find this Wikibook article on I/O in Python to be a useful reference as well.

    评论

报告相同问题?

悬赏问题

  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件