简体   繁体   中英

Mulit-Line User Input Python3

I would like to have a user input in python, which is not only one line. Furthermore, I don't know how many lines there will be. Example:

line1...

line2...

line3...

I've already tried this:

lines = []
try:
    while True:
        line = input()
        if line:
            line = line.split(" ")
            lines.append(line)
        else:
            break
except EOFError:
    pass

The problem with this is, that when I have an empty line it doesn't work anymore.

The indicator that the user is done is the problem. I want a text to be pasted in as user input and after that it should stop.

Thanks for your replies!

You could try something like this:

lines = []
while True:
    inp = input()
    if inp:
        lines.append(inp)
    else:
        break

You have multiple solutions to pass multiple lines as arguments to your Python program.

FIRST SOLUTION:

You can invoke a python program and process the standard input using the input method. This allows you to process any number of lines and the content is added while your program is running. However, the users must have a way to indicate that the input has finished (a line containing "EOF", an empty line, etc.) in order to allow the program to stop looking for more input and begin other processes.

$ cat prog3.py
def main():
    # Retrieve input
    lines = []
    line = input("Enter first line or empty line to end\n")
    lines.append(line)
    while line:                                                                                                     
        line = input("Enter next line or empty line to end\n")
        lines.append(line)

    # Process input lines
    for l in lines:
        print(l)

    # Process input lines
    for l in lines:
        print(l)

if __name__ == "__main__":
    main()

$ python prog.py
Enter first line or empty line to end
line1 # Written by user
Enter next line or empty line to end
line2 # Written by user
Enter next line or empty line to end
line3 # Written by user
Enter next line or empty line to end

line1
line2
line3

SECOND SOLUTION:

You can dump all the lines to a file, pass the file as an argument to your python program and process each line of the file separately. Notice that this method also requires to know the number of lines and their content before invoking the python program.

$ cat prog2.py
def main():
    import sys
    file_name = str(sys.argv[1])
    with open(file_name, 'r') as f:
        for l in f:
            print(l)

if __name__ == "__main__":
    main()

$ cat 1.in
line1
line2
line3

$ python prog.py "1.in"
line1
line2
line3

THRID SOLUTION:

If you want to use the regular python sys.argv you need to wrap all the lines as a string, adding a delimiter between lines, and pass them as a single parameter to your python program. Then, in the python program, you need to parse the string into multiple lines and proceed one by one.

This feels a little cheating for me but it might come useful in some use cases.

Notice that this method requires you to know the number of lines and their content before invoking the python program.

$ cat prog1.py
def main():
    import sys
    lines = sys.argv[1].split("|")
    for l in lines:
        print(l)

if __name__ == "__main__":
    main()

$ python prog.py "line1|line2|line3"
line1
line2
line3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM