How do you read from stdin?

后端 未结 22 2634
余生分开走
余生分开走 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:21

    This will echo standard input to standard output:

    import sys
    line = sys.stdin.readline()
    while line:
        print line,
        line = sys.stdin.readline()
    
    0 讨论(0)
  • 2020-11-21 07:21

    argparse is an easy solution

    Example compatible with both Python versions 2 and 3:

    #!/usr/bin/python
    
    import argparse
    import sys
    
    parser = argparse.ArgumentParser()
    
    parser.add_argument('infile',
                        default=sys.stdin,
                        type=argparse.FileType('r'),
                        nargs='?')
    
    args = parser.parse_args()
    
    data = args.infile.read()
    

    You can run this script in many ways:

    1. Using stdin

    echo 'foo bar' | ./above-script.py
    

      or shorter by replacing echo by here string:

    ./above-script.py <<< 'foo bar'
    

    2. Using a filename argument

    echo 'foo bar' > my-file.data
    ./above-script.py my-file.data
    

    3. Using stdin through the special filename -

    echo 'foo bar' | ./above-script.py -
    
    0 讨论(0)
  • 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()

    0 讨论(0)
  • 2020-11-21 07:28

    The problem I have with solution

    import sys
    
    for line in sys.stdin:
        print(line)
    

    is that if you don't pass any data to stdin, it will block forever. That's why I love this answer: check if there is some data on stdin first, and then read it. This is what I ended up doing:

    import sys
    import select
    
    # select(files to read from, files to write to, magic, timeout)
    # timeout=0.0 is essential b/c we want to know the asnwer right away
    if select.select([sys.stdin], [], [], 0.0)[0]:
        help_file_fragment = sys.stdin.read()
    else:
        print("No data passed to stdin", file=sys.stderr)
        sys.exit(2)
    
    0 讨论(0)
  • 2020-11-21 07:31

    Here's from Learning Python:

    import sys
    data = sys.stdin.readlines()
    print "Counted", len(data), "lines."
    

    On Unix, you could test it by doing something like:

    % cat countlines.py | python countlines.py 
    Counted 3 lines.
    

    On Windows or DOS, you'd do:

    C:\> type countlines.py | python countlines.py 
    Counted 3 lines.
    
    0 讨论(0)
  • 2020-11-21 07:32
    import sys
    
    for line in sys.stdin:
        print(line)
    

    Note that this will include a newline character at the end. To remove the newline at the end, use line.rstrip() as @brittohalloran said.

    0 讨论(0)
提交回复
热议问题