Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: padding punctuation with white spaces (keeping punctuation)

What is an efficient way to pad punctuation with whitespace?

input:

s = 'bla. bla? bla.bla! bla...'

desired output:

 s = 'bla . bla ? bla . bla ! bla . . .'

Comments:

  1. I don't care how many whitespaces are there between tokens. (but they'll need to be collapsed eventually)
  2. I don't want to pad all punctuation. Say I'm interested only in .,!?().
like image 543
ScienceFriction Avatar asked Sep 05 '10 11:09

ScienceFriction


People also ask

How do you put a space before punctuation in Python?

To add space after dot or comma using replace() in Python' with ', ' or '.

How do you replace all punctuation with space in Python?

Use regex to Strip Punctuation From a String in Python The regex pattern [^\w\s] captures everything which is not a word or whitespace(i.e. the punctuations) and replaces it with an empty string.

Does string punctuation include space?

Note The string. punctuation values do not include Unicode symbols or whitespace characters. Remove punctuation.

How do you remove punctuation from a string in Python?

One of the easiest ways to remove punctuation from a string in Python is to use the str. translate() method. The translate() method typically takes a translation table, which we'll do using the . maketrans() method.


2 Answers

You can use a regular expression to match the punctuation characters you are interested and surround them by spaces, then use a second step to collapse multiple spaces anywhere in the document:

s = 'bla. bla? bla.bla! bla...'
import re
s = re.sub('([.,!?()])', r' \1 ', s)
s = re.sub('\s{2,}', ' ', s)
print(s)

Result:

bla . bla ? bla . bla ! bla . . .
like image 92
Mark Byers Avatar answered Oct 26 '22 20:10

Mark Byers


If you use python3, use the maketrans() function.

import string   
text = text.translate(str.maketrans({key: " {0} ".format(key) for key in string.punctuation}))
like image 24
Lynne Avatar answered Oct 26 '22 21:10

Lynne