csdnceshi62 2009-09-20 05:48 采纳率: 100%
浏览 1680

如何在 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

  • 写回答

17条回答 默认 最新

  • 狐狸.fox 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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!