python - how to make punctuation a separate item when using split() -
i'm writing program compresses text replicating sequence of numbers - don't know how program recognise punctuation separate item in list.
eg, in sentence comma, comma means words 'comma,' , 'comma' different when using split(). want have 'comma' ',' 'comma' instead.
i don't want rid of punctuation - want separate item in list
you can use re.split this:
>>> re.split('([{}])'.format(re.escape(string.punctuation)), "comma,comma") ['comma', ',', 'comma']
Comments
Post a Comment