Python - how to separate paragraphs from text?
up vote
0
down vote
favorite
I need to separate texts into paragraphs and be able to work with each of them. How can I do that? Between every 2 paragraphs can be at least 1 empty line. Like this:
Hello world,
this is an example.
Let´s program something.
Creating new program.
Thanks in advance.
python paragraph
New contributor
|
show 1 more comment
up vote
0
down vote
favorite
I need to separate texts into paragraphs and be able to work with each of them. How can I do that? Between every 2 paragraphs can be at least 1 empty line. Like this:
Hello world,
this is an example.
Let´s program something.
Creating new program.
Thanks in advance.
python paragraph
New contributor
Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
– raj
Nov 10 at 16:10
This is clear for me, but I need a help with syntax, how to write this.
– kom20
Nov 10 at 16:14
1
@kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
– Jon Clements♦
Nov 10 at 16:17
I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
– kom20
Nov 10 at 16:21
Usestr.splitlines()
– snakecharmerb
Nov 10 at 16:30
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to separate texts into paragraphs and be able to work with each of them. How can I do that? Between every 2 paragraphs can be at least 1 empty line. Like this:
Hello world,
this is an example.
Let´s program something.
Creating new program.
Thanks in advance.
python paragraph
New contributor
I need to separate texts into paragraphs and be able to work with each of them. How can I do that? Between every 2 paragraphs can be at least 1 empty line. Like this:
Hello world,
this is an example.
Let´s program something.
Creating new program.
Thanks in advance.
python paragraph
python paragraph
New contributor
New contributor
edited Nov 10 at 21:02
user6910411
31.6k76592
31.6k76592
New contributor
asked Nov 10 at 16:02
kom20
6
6
New contributor
New contributor
Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
– raj
Nov 10 at 16:10
This is clear for me, but I need a help with syntax, how to write this.
– kom20
Nov 10 at 16:14
1
@kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
– Jon Clements♦
Nov 10 at 16:17
I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
– kom20
Nov 10 at 16:21
Usestr.splitlines()
– snakecharmerb
Nov 10 at 16:30
|
show 1 more comment
Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
– raj
Nov 10 at 16:10
This is clear for me, but I need a help with syntax, how to write this.
– kom20
Nov 10 at 16:14
1
@kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
– Jon Clements♦
Nov 10 at 16:17
I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
– kom20
Nov 10 at 16:21
Usestr.splitlines()
– snakecharmerb
Nov 10 at 16:30
Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
– raj
Nov 10 at 16:10
Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
– raj
Nov 10 at 16:10
This is clear for me, but I need a help with syntax, how to write this.
– kom20
Nov 10 at 16:14
This is clear for me, but I need a help with syntax, how to write this.
– kom20
Nov 10 at 16:14
1
1
@kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
– Jon Clements♦
Nov 10 at 16:17
@kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
– Jon Clements♦
Nov 10 at 16:17
I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
– kom20
Nov 10 at 16:21
I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
– kom20
Nov 10 at 16:21
Use
str.splitlines()
– snakecharmerb
Nov 10 at 16:30
Use
str.splitlines()
– snakecharmerb
Nov 10 at 16:30
|
show 1 more comment
3 Answers
3
active
oldest
votes
up vote
1
down vote
This sould work:
text.split('nn')
Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
– kom20
Nov 10 at 18:00
This is for you to say. You can always filter them out withfilter(None, ...)
– roeen30
Nov 10 at 18:26
add a comment |
up vote
0
down vote
Try
result = list(filter(lambda x : x != '', text.split('nn')))
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
up vote
0
down vote
I usually strip before split then filter out the ''. ;)
a =
'''
Hello world,
this is an example.
Let´s program something.
Creating new program.
'''
data = [content for content in a.strip().splitlines() if content]
print(data)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This sould work:
text.split('nn')
Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
– kom20
Nov 10 at 18:00
This is for you to say. You can always filter them out withfilter(None, ...)
– roeen30
Nov 10 at 18:26
add a comment |
up vote
1
down vote
This sould work:
text.split('nn')
Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
– kom20
Nov 10 at 18:00
This is for you to say. You can always filter them out withfilter(None, ...)
– roeen30
Nov 10 at 18:26
add a comment |
up vote
1
down vote
up vote
1
down vote
This sould work:
text.split('nn')
This sould work:
text.split('nn')
answered Nov 10 at 17:00
roeen30
20419
20419
Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
– kom20
Nov 10 at 18:00
This is for you to say. You can always filter them out withfilter(None, ...)
– roeen30
Nov 10 at 18:26
add a comment |
Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
– kom20
Nov 10 at 18:00
This is for you to say. You can always filter them out withfilter(None, ...)
– roeen30
Nov 10 at 18:26
Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
– kom20
Nov 10 at 18:00
Thanks, it seems good. But since the end of the text consists of some empty lines, last items in this list are empty (like this): ["something","",""]. Can this make any problem as soon as I get into work with the particular words in these paragraphs?
– kom20
Nov 10 at 18:00
This is for you to say. You can always filter them out with
filter(None, ...)
– roeen30
Nov 10 at 18:26
This is for you to say. You can always filter them out with
filter(None, ...)
– roeen30
Nov 10 at 18:26
add a comment |
up vote
0
down vote
Try
result = list(filter(lambda x : x != '', text.split('nn')))
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
up vote
0
down vote
Try
result = list(filter(lambda x : x != '', text.split('nn')))
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
up vote
0
down vote
up vote
0
down vote
Try
result = list(filter(lambda x : x != '', text.split('nn')))
Try
result = list(filter(lambda x : x != '', text.split('nn')))
edited Nov 10 at 21:14
answered Nov 10 at 21:05
bolzano
3621827
3621827
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
While this might answer the authors question, it lacks some explaining words and/or links to documentation. Raw code snippets are not very helpful without some phrases around them. You may also find how to write a good answer very helpful. Please edit your answer.
– hellow
Nov 11 at 7:21
add a comment |
up vote
0
down vote
I usually strip before split then filter out the ''. ;)
a =
'''
Hello world,
this is an example.
Let´s program something.
Creating new program.
'''
data = [content for content in a.strip().splitlines() if content]
print(data)
add a comment |
up vote
0
down vote
I usually strip before split then filter out the ''. ;)
a =
'''
Hello world,
this is an example.
Let´s program something.
Creating new program.
'''
data = [content for content in a.strip().splitlines() if content]
print(data)
add a comment |
up vote
0
down vote
up vote
0
down vote
I usually strip before split then filter out the ''. ;)
a =
'''
Hello world,
this is an example.
Let´s program something.
Creating new program.
'''
data = [content for content in a.strip().splitlines() if content]
print(data)
I usually strip before split then filter out the ''. ;)
a =
'''
Hello world,
this is an example.
Let´s program something.
Creating new program.
'''
data = [content for content in a.strip().splitlines() if content]
print(data)
answered Nov 10 at 21:25
Prayson Daniel
1,0381817
1,0381817
add a comment |
add a comment |
kom20 is a new contributor. Be nice, and check out our Code of Conduct.
kom20 is a new contributor. Be nice, and check out our Code of Conduct.
kom20 is a new contributor. Be nice, and check out our Code of Conduct.
kom20 is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240763%2fpython-how-to-separate-paragraphs-from-text%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Assuming the text is in a text file. Read the file line wise and whenever you encounter a blank line, you know that whatever was above that line belonged to a paragraph. Extend this similarly for upcoming text.
– raj
Nov 10 at 16:10
This is clear for me, but I need a help with syntax, how to write this.
– kom20
Nov 10 at 16:14
1
@kom20 do you know how to open a file and read a line? What difficultly do you have specifically ?
– Jon Clements♦
Nov 10 at 16:17
I know this, but I need to align all paragraphs for set width of characters and for that I need to separate paragraphs from the text and work with each individually.
– kom20
Nov 10 at 16:21
Use
str.splitlines()
– snakecharmerb
Nov 10 at 16:30