8

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

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

When using Enthought's 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. How can I fix this?

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)
6
  • 1
    How does it know when to stop reading input? A blank line, a stopword, something like that? Jan 19, 2016 at 23:55
  • Added some more relavent data. I don't know how it knows to stop reading the input? Jan 20, 2016 at 0:02
  • raw_input and input stop waiting for keyboard input on a newline character, which you get by pressing Enter. That newline is not included in the output. If you want your data to include newline characters, you will have to determine how you want to tell the program to stop waiting for input, and then have the program keep asking for input until this condition is met. Jan 20, 2016 at 0:23
  • @AdamSchulz Welcome to StackOverflow. Even though your question has been asked in a different way before, you can still accept or vote for answers you found helpful.
    – user764357
    Jan 20, 2016 at 0:46
  • Is the code (as posted) actually syntactically correct? Jan 13 at 22:22

1 Answer 1

5

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.

4
  • 1
    Ctrl-D doesn't stop input in my shell. Jan 20, 2016 at 0:26
  • What shell do you use?
    – user764357
    Jan 20, 2016 at 0:30
  • Tried it on both cmd and Powershell. Jan 20, 2016 at 0:32
  • 1
    Windows has a different "EOF" key command, I've updated the answer.
    – user764357
    Jan 20, 2016 at 0:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.