簡體   English   中英

如何在jupyter-notebook中不隨意添加`\n`復制粘貼多行文本輸入?

[英]How to copy paste multi-line text input without arbitrarily adding `\n` in jupyter-notebook?

我正在嘗試從 jupyter-notebook 單元格獲取交互式多行用戶輸入,如下所示:

contents = []
while True:
    line = input()
    if line:
        contents.append(line)
    else:
        break
        
input_text = "\n".join(contents)

輸入文本示例:

This is a multi-line input.
It is good to put several lines after each other. 
The reason of this is a good indentation.
One can use both tabs and spaces.

但是,當我從某處復制粘貼多行輸入而不是手動逐行傳遞時,它會連接成一個空格分隔的字符串,如下所示:

在此處輸入圖像描述

如何建立復制粘貼多行文本輸入而不在每行后任意添加\n

如果您能夠通過添加 '\r\n' 或其他可以在之后拆分的預定義分隔符來預處理用戶輸入,則可能會出現骯臟的解決方法

This is a multi-line input.\r\n
It is good to put several lines after each other.\r\n
The reason of this is a good indentation.\r\n
One can use both tabs and spaces.

你可以使用解碼

print(input().encode().decode('unicode_escape'))

結果 output:

This is a multi-line input.
It is good to put several lines after each other.
The reason of this is a good indentation.
One can use both tabs and spaces.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM