I have a code in which the user should pass something through raw_input, this works fine for strings less then one line,
but when I copy-paste a longer string, only the first line is passed by raw_input, how can I also past the rest of the string?

Recommended Answers

All 7 Replies

You can read multiple lines with multiple raw_input and have a function which tests if input is complete. Here is an example

def input_complete(input_list):
    if ";" in input_list[-1]:
        return True
    else:
        return False

def get_input(prompt1, prompt2):
    L = list()
    prompt = prompt1
    while True:
        L.append(raw_input(prompt))
        if input_complete(L):
            return "\n".join(L)
        prompt = prompt2

if __name__ == "__main__":
    s = get_input("enter data: ", "? ")
    print repr(s)

Here, the input is terminated by a line which contains a ';'. Here is an example session

enter data: This is an example
? of a multiline 
? input, using the function
? get_input and the ; rule
'This is an example\nof a multiline\ninput, using the function\nget_input and the ; rule'

You only need to define your own rule to check for input completion.

I see, can you replace the colon with the enter?

caus the problem only occurs when I copy-paste a long string, if I just type it, it will work without your function =)

It will work only if you are expecting input with a minimal structure so that you can define a rule to test if the input is complete or if you must wait for another line. You can't replace the colon by enter because when you paste a text, raw input will read only up to the first '\n' in the text.

hm, i will google a bit more to find a workaround for the copy-paste problem without using your function, because the input can contain any character so I can't choose which one I should use as filter

Another solution would be to paste your string in a GUI's entry field or text widget instead of the command line.

Yes indeed, I was planning to build a GUI anyway, but it would be nice if I also had a command line utility =(
but I'm afraid there is no other solution

It's really odd, with some strings copy-pasting does work and with others it don't =(

I think those that fail all have in common that they have a backslash..?

how can I solve this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.