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

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -