循环通过无限的多行输入

发布于 2025-02-13 21:32:49 字数 740 浏览 2 评论 0原文

想象无限

”输入的URL地址pycharm”

您将如何分别从输入中获得该地址?

接近,但实际上没有帮助:

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

如何从用户获得多行输入

和其他建议我完全没有用: https://python.plainenglish.io/taking-multiple-inputs-from-user-in-python-3-6cbe02d03a95

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蛮可爱 2025-02-20 21:32:49

如果我正确理解问题,这可能会有所帮助:

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

这将处理交互式和重定向输入(STDIN)

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)

2025-02-20 21:32:49

您可以使用一个函数:

import sys
sys.stdin.read()

但是我不确定这是一个好主意,因为它不知道何时停止阅读。传统input()函数将通过返回键检测\ n符号,然后结束输入。

我认为循环是前进的最佳方法。

喜欢:

urls = ''

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

祝你有美好的一天。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文