How do you read from stdin?

后端 未结 22 3031
余生分开走
余生分开走 2020-11-21 06:46

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?

22条回答
  •  不要未来只要你来
    2020-11-21 07:22

    You could use the fileinput module:

    import fileinput
    
    for line in fileinput.input():
        pass
    

    fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided.

    Note: line will contain a trailing newline; to remove it use line.rstrip()

提交回复
热议问题