我有raw_input命令的問題。我在程序中使用它,並在cmd中運行程序。然後它詢問輸入,但是當我粘貼其中有空行的文本時,則Python自動將其作爲輸入命令。我想讓它明白它不是輸入命令,而是新行。我怎麼能這樣做?cmd中的raw_input Python
0
A
回答
1
要做到這一點,您不能使用raw_input
函數,因爲此函數在第一個新行字符後完成讀取輸入。相反,你可以使用sys.stdin
:
>>>import sys
>>>input = sys.stdin.read()
hi
this text has
new lines
Ctrl^D
>>>print input
hi
this text has
new lines
讀完輸入你要發送EOF字符,它在Linux是按Ctrl +d和Windows是按Ctrl +ž
希望這會有所幫助!
0
根據python文檔,raw_input()被設計爲具有\n
作爲結束輸入的關鍵,並且它沒有被設計用於另一種交互方式。
如果您想使用另一種使用輸入的方式,您可以嘗試使用fileinput()
或sys.stdin.read()
,並遍歷線條或字符並創建要查找的行爲。
看一看的問題的答案,如that one
相關問題
- 1. 中的raw_input Python變量到CMD
- 2. raw_input python
- 3. raw_input Python
- 4. Python raw_input()
- 5. 的raw_input()在Python
- 6. Python中的線程+ raw_input
- 7. 回顧Python中的raw_input/input
- 8. Python中的raw_input函數
- 9. raw_input()中的Python EOF錯誤
- 10. python隱藏raw_input
- 11. Python:改變raw_input?
- 12. python raw_input()syntax
- 13. Python:掃描raw_input
- 14. Python raw_input(「」)錯誤
- 15. python中添加raw_input數
- 16. Python EOF錯誤raw_input()
- 17. Python,輸入和raw_input
- 18. python raw_input不工作
- 19. Python:使raw_input執行
- 20. Python 2.7 raw_input腳本
- 21. 的raw_input的Python發行
- 22. Python-沒有的raw_input工作
- 23. 特定範圍的Python Raw_Input
- 24. 使用raw_input的Python中的EOF錯誤
- 25. if語句的raw_input和Python中
- 26. 從python中的raw_input打印結果
- 27. python中的raw_input沒有按下輸入
- 28. Python 2.7 bug中的raw_input輸出「None」
- 29. Google App Engine shell中的Python raw_input
- 30. 設置函數調用python中的raw_input
很難明白到底是什麼導致你的問題沒有看到一些代碼。你能否加入一個簡短的程序(10行左右)來重現問題? –
第一次搜索給了我:[如何處理原始數據的蟒蛇](http://stackoverflow.com/questions/19202407/how-to-process-raw-data-in-python#answer -19202500) –