[Tutor] Splitting on punctuation
Mike Nickey
mnickey at gmail.com
Sun Jun 9 01:44:29 CEST 2013
Hey guys,
I'm working on a web-project simply to bruh up on skills and build new
ones.
One of the tasks is to split on punctuation passed yet I'm having a bit
more trouble then I expected.
Give the input of "which isn't that surprising I guess.",",'.") where the
first part passed is the string and the second part is the punctuation to
split on, I'm having difficulty converting the punctuation to a split
parameter.
As you'll see I have tried various attempts at replace, strip and split but
I can't seem to get it just right.
Currently I have the following:
import string
def tokenize_query(query, punctuation):
# informational and to be removed
print 'Query passed: ', query
print 'Punctuation passed:' , punctuation
print '-----------------------'
punc = punctuation
query = query.replace(punc," ")
words = query.split()
for item in words:
item = item.replace(punc,' ')
# item = item.rstrip(punctuation)
# item = item.replace(punctuation,"")
print item
tokenize_query("Dynamic programming string processing algorithms","")
print
tokenize_query("The quick! bro'wn fox! runs slowly$",",$'")
print
tokenize_query("which isn't that surprising I guess.",",'.")
The output I get is:
Query passed: which isn't that surprising I guess.
Punctuation passed: ,'.
-----------------------
which
isn't
that
surprising
I
guess.
What I should have is:
Query passed: which isn't that surprising I guess.
Punctuation passed: ,'.
-----------------------
which
isn
t
that
surprising
I
guess
Any suggestions? and thanks in advance.
--
~MEN
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130608/c4c21dbb/attachment.html>
More information about the Tutor
mailing list