Noivern Evo Noivern Evo - 1 year ago 68
Python Question

How can I read multiple lines from a text area using a python API script?

I'm currently working on a python script and part of the input I'm getting is a text area. The user inputs a multi-line value and I need to use each line separately. So the input from the user would look something like this:


And I need to put them each in seperate vars in the python script. How would I go about reading the input line-by-line?

Answer Source

If the input is a string separated by a new-line character you could use the splitlines() method:

text = 'value1\nvalue2\nvalue3\nvalue4'
var1, var2, var3, var4 = text.splitlines()

This will put them in separate variables but you'll get an error if there's less or more than four lines. Thus it's safer to put it in a list:

text = 'value1\nvalue2\nvalue3\nvalue4'
list_of_vars = text.splitlines()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download