Split a sentence by punctuations using Python

C

chad

I want to split sentences by using punctuations, numeric numbers as
the delimiters.

For example, suppose I have a text that contains sentences like so:

"To help you get there a bit faster, I will be driving at 120 miles an
hour (I am just kidding). Is that OK?"

Now, I want to get the following segments from this text:

To help you get there a bit faster
I will be driving at
miles an hour
I am just kidding
Is that OK

You know, string.split does not work for this objective. So how can i
achieve this? Thanks.
 
S

Skip Montanaro

chad> You know, string.split does not work for this objective. So how
chad> can i achieve this?

Check the re module docs.

Skip
 
P

Peter Otten

chad said:
I want to split sentences by using punctuations, numeric numbers as
the delimiters.

For example, suppose I have a text that contains sentences like so:

"To help you get there a bit faster, I will be driving at 120 miles an
hour (I am just kidding). Is that OK?"

Now, I want to get the following segments from this text:

To help you get there a bit faster
I will be driving at
miles an hour
I am just kidding
Is that OK

You know, string.split does not work for this objective. So how can i
achieve this? Thanks.
'To help you get there a bit faster, I will be driving at 120 miles an hour
(I am just kidding). Is that OK?'
r = re.compile("[,.?()\\d]+ *")
print "\n".join(r.split(s))
To help you get there a bit faster
I will be driving at
miles an hour
I am just kidding
Is that OK

Is this OK? Note the trailing empty line. If this is not desired, just
remove the last list item if empty.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top