0

Imagine unlimited multiline input with some URL addresses, for example:

inputed URL addresses in PyCharm

How would you get that addresses from the input separately?

Following was close, but actually not helping:

lines = []
while True:
    line = input()
    if line:
        lines.append(line)
    else:
        break
text = '\n'.join(lines)

source: How to get multiline input from user

And other advices I consider completely useless: https://python.plainenglish.io/taking-multiple-inputs-from-user-in-python-3-6cbe02d03a95

pavel
  • 15
  • 5

2 Answers2

0

If I understand the problem correctly, this may help:

lines = []
try:
    while line := input():
        lines.append(line)
except EOFError:
    pass
print(lines)

This will handle both interactive and redirected input (stdin)

DarkKnight
  • 19,739
  • 3
  • 6
  • 22
  • The problem is, that while loop will start over again with new input request. – pavel Jul 07 '22 at 18:39
  • This works exactly as requested. You could copy and paste, or enter free text. Not sure what issue you are having here. – chitown88 Jul 08 '22 at 08:26
0

There is a function you could use:

import sys
sys.stdin.read()

But I'm not sure this is a great idea because it won't know when to stop reading. The traditional input() function will detect a \n symbol via the return key, then end input.

I think looping would be the best way forward.

Like:

urls = ''

while True:
    url = input()
    
    if url:
        urls += url + '\n'
    else:
        break

Have a nice day.

Jude Davis
  • 98
  • 1
  • 5