I have a form with:
<textarea name="test">
Assuming the user inputs the following text:
This is the first paragraph
It has two lines
This is the second paragraph
I would like to split this text into a list [“This is the first paragraph\nIt has two lines”, “This is the second paragraph”]
I thought this would work:
temp = self.request.get("test")
list = re.split(r'\n\n', temp)
But it does not. However,
temp = self.request.get("test")
list = re.split(r'\n', temp)
yields the following list: [“This is the first line”, “”, “This is the second line”]
What am I missing?
ALSO:
Assuming there could be either one or two empty lines between the to texts, would this make sense?
temp = self.request.get("test")
list = re.split(r'(\n){2,3}', temp)
SOLUTION:
With the help below,
I’ve found out that the following code works in my case:
temp = self.request.get("test")
list = [l for l in temp.split('\r\n\r\n') if l.split()]
I think the line-breaking might depend on what system the input comes from, so it might not be the perfect solution.
I think
remodule might be overkill. Just split the content by\nand remove the empty strings.The
string.splitalso appears to be about twice as fast.