user2553807
user2553807

Reputation: 329

Python split punctuation but still include it

This is the list of strings that I have:

 [
  ['It', 'was', 'the', 'besst', 'of', 'times,'], 
  ['it', 'was', 'teh', 'worst', 'of', 'times']
 ]

I need to split the punctuation in times,, to be 'times',','
or another example if I have Why?!? I would need it to be 'Why','?!?'

import string

def punctuation(string):

for word in string:
    if word contains (string.punctuation):
        word.split()

I know it isn't in python language at all! but that's what I want it to do.

Upvotes: 0

Views: 672

Answers (4)

Jiminion
Jiminion

Reputation: 5168

Something like this? (Assumes punct is always at end)

def lcheck(word):
    for  i, letter in enumerate(word):
        if not word[i].isalpha():
            return [word[0:(i-1)],word[i:]]
    return [word]

value = 'times,'
print lcheck(value)

Upvotes: 0

dansalmo
dansalmo

Reputation: 11686

A generator solution without regex:

import string
from itertools import takewhile, dropwhile

def splitp(s):
    not_punc = lambda c: c in string.ascii_letters+"'"  # won't split "don't"
    for w in s:
        punc = ''.join(dropwhile(not_punc, w))
        if punc:
            yield ''.join(takewhile(not_punc, w))
            yield punc
        else:
            yield w

list(splitp(s))

Upvotes: 0

zhangyangyu
zhangyangyu

Reputation: 8610

You can use finditer even if the string is more complex.

    >>> r = re.compile(r"(\w+)(["+string.punctuation+"]*)")
    >>> s = 'Why?!?Why?*Why'
    >>> [x.groups() for x in r.finditer(s)]
    [('Why', '?!?'), ('Why', '?*'), ('Why', '')]
    >>> 

Upvotes: 3

Conan Li
Conan Li

Reputation: 494

you can use regular expression, for example:

In [1]: import re

In [2]: re.findall(r'(\w+)(\W+)', 'times,')
Out[2]: [('times', ',')]

In [3]: re.findall(r'(\w+)(\W+)', 'why?!?')
Out[3]: [('why', '?!?')]

In [4]: 

Upvotes: 1

Related Questions