Python 有没有办法跳过input()中的换行符?

Python 有没有办法跳过input()中的换行符?,python,python-3.x,python-2.7,function,Python,Python 3.x,Python 2.7,Function,问题是,当粘贴段落以获得结果时,functionprogram无法在换行后处理句子,因为它只在第一行执行,而不是在第二行执行 试试这一段,不包括--开始--或--结束-- 创建的输入仅读取一行。若要阅读更多内容,您必须在循环中使用输入,但当文本结束时,您必须以某种方式通知循环-例如,您可以在段落后放置空行 lines = [] while True: line = input() if not line: # exit loop when empty line.

问题是,当粘贴段落以获得结果时,functionprogram无法在换行后处理句子,因为它只在第一行执行,而不是在第二行执行

试试这一段,不包括--开始--或--结束--

创建的输入仅读取一行。若要阅读更多内容,您必须在循环中使用输入,但当文本结束时,您必须以某种方式通知循环-例如,您可以在段落后放置空行

lines = []

while True:
    line = input()
    if not line: # exit loop when empty line.
        break
    lines.append(line)

text = '\n'.join(lines)

print('>', text, '<')
使用sys.stdin.read可以将其写得更短

import sys

try:
    text = sys.stdin.read()  # press `Ctrl+D` to end of data
except EOFError:
    pass

print('>', text, '<')
它还应该适用于从文件重定向的文本

python script.py < file_with_paragrph.txt

输入总是只得到一行。如果需要更多,则必须使用多次使用输入的循环。无法理解,请指定codewhile True:lines+=输入,但需要方法来识别数据的结尾。它可以是空行。请回答,仍然困惑为什么要从输入中排除开始和结束标记?你需要一些方法让你的程序知道哪一行属于一个段落,而这样的标记是最理想的。
lines = []

while True:
    try:
        line = input()  # press `Ctrl+D` to end of data
        lines.append(line)
    except EOFError:
        break

text = '\n'.join(lines)

print('>', text, '<')
import sys

try:
    text = sys.stdin.read()  # press `Ctrl+D` to end of data
except EOFError:
    pass

print('>', text, '<')
import sys

text = sys.stdin.read()  # press `Ctrl+D` to end of data

print('>', text, '<')
python script.py < file_with_paragrph.txt