14

I have a list of items which look like so:

 2.4       -2.0           4.3
-6.0       12.5           1.0

What I would like is to remove all those spaces and replace them with "," (comma) except for the spaces in front of first numbers (they should be just deleted (the spaces) and not replaced with anything). So the upper string items should look like so, after replacement:

2.4,-2.0,4.3
-6.0,12.5,1.0

Not like this:

,2.4,-2.0,4.3
,-6.0,12.5,1.0

Which is what the following code does:

newStrings = []
for s in strings:
    newStrings.append(re.sub('\s+', ',', s))

What regular expression for re.sub should be used to achieve that? Thank you.

Thibaut
  • 1,398
  • 10
  • 16
marco
  • 899
  • 5
  • 13
  • 21

2 Answers2

26

To remove the leading and trailing spaces you can use .strip(), and then to replace consecutive whitespace characters using the regular expression \s+:

>>> import re
>>> s = " 2.4       -2.0           4.3"
>>> re.sub("\s+", ",", s.strip())
'2.4,-2.0,4.3'
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
  • Thank you Daniel. Is there some beginner friendly tutorial on regular expression? I tried this one, but looks to complicated for my level of knowledge: http://www.tutorialspoint.com/python/python_reg_expressions.htm – marco Nov 15 '14 at 19:33
  • There are many, but one written for Python is https://docs.python.org/2/howto/regex.html. – DanielGibbs Nov 15 '14 at 19:36
  • 2
    Checked that one too. Sometimes I have a feeling that people who do not want other people to learn Python, write these kinds of documentations. Thank you though. – marco Nov 15 '14 at 19:42
  • No problem. If the answer solved your problem would you mind marking it as the accepted answer? (Click the check mark) – DanielGibbs Nov 15 '14 at 19:44
  • Any way to do this but not lose the end of line? – Biaspoint Oct 01 '22 at 16:22
  • @Biaspoint You could just add `+ "\n"` to the end of the line to add the missing newline. – DanielGibbs Oct 04 '22 at 00:06
  • @DanielGibbs I'll probably have to just ask a new question, but it sounds like you are implying manually adding in a ```\n```? Was hoping for a programmatic solution (sorry if I'm not understanding you correctly)... in other words, how would this change ```re.sub("\s+", ",", s.strip())``` – Biaspoint Oct 04 '22 at 12:53
  • 1
    @Biaspoint You could just do `re.sub("\s+", ",", s.strip()) + "\n"`, unless your string has multiple newlines, in which case you could do `re.sub("[^\S\r\n]+", ",", s.strip())`. Regex taken from https://stackoverflow.com/a/3469155/343486 – DanielGibbs Oct 04 '22 at 22:59
0

There are many solutions... This doesn't even briefly cover the whole topic, but it works:

Quick solution:

In [1]: import re
   ...: d_in = "strip \t\r\n\x00er \ter\rMY\   nr\x00 \t\r\nSPAC       ES\x00  ! "
   ...: d_out = re.sub("\s+", r",", d_in)
   ...: d_out
   ...: 
Out[1]: 'strip,\x00er,er,MY\\,nr\x00,SPAC,ES\x00,!,'
internety
  • 364
  • 3
  • 8