How to paste multiple lines of text into python input [duplicate]

廉价感情. 提交于 2021-01-21 09:10:00

问题


I currently have the following line of code for an input:

rawdata = raw_input('please copy and paste your charge discharge data')

When using Enthoughts GUI with Ipython and I run my script I can copy and paste preformatted text fine which pulls with it the \t and \n. When trying to paste data into a terminal style version of the script it however tries to process each line of data rather than accepting it as bulk. Any help?

More relevant lines of code:

rawed = raw_input('Please append charge data here: ')     
    time, charge = grab_and_sort(rawed)

def grab_and_sort(rawdata):

    rawdata = rawdata.splitlines()
    ex = []
    why = []

    for x in range(2 , len(rawdata)):  
        numbers = rawdata[x].split('\t')
        ex.append(numbers[0])
        why.append(numbers[1])

    ex = array(ex)    
    why = array(why)  

    return (ex, why)

回答1:


raw_input accepts any input up until a new line character is entered.

The easiest way to do what you are asking it to accept more entries, until an end of file is encountered.

print("please copy and paste your charge discharge data.\n"
      "To end recording Press Ctrl+d on Linux/Mac on Crtl+z on Windows")
lines = []
try:
    while True:
        lines.append(raw_input())
except EOFError:
    pass
lines = "\n".join(lines)

Then do something with the whole batch of text.



来源:https://stackoverflow.com/questions/34889012/how-to-paste-multiple-lines-of-text-into-python-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!