By Kingswoop

2020-06-30 03:35:38 8 Comments

Here is a string:

.      68.00     68.00          .                                        

I am trying to remove the first . and the fourth. while adding a comma between.

output should look like:


Have tried strip and some initial character removal functions but having issues e.g. [1:] Any help would be appreciated.


@Lucius Kaye 2020-06-30 03:54:24

You can do a simple (yet convoluted looking) regex search and replace. You may have to adjust this regex to your needs.

import re

the_input = '.      68.00       68.00          .     '

# r = raw string
# the_regex = r'\s*\.\s*(\d+)\s+(\d+)\s+\.\s+'
the_regex = r'\s*\.\s*(\d+\.\d+)\s+(\d+\.\d+)\s+\.\s+' # with decimals numbers
the_output = re.sub(the_regex, r'\1,\2', the_input)

the_regex is a bit convoluted so here is it broken down.

  • \s* - spaces, 0 or more
  • \. - one dot
  • (\d+) - 1st pair of parenthesis means capture group 1. 2nd pair is 2nd group.
  • \d+ - number digits, 1 or more
  • \s+ - spaces, 1 or more

In re.sub(), \1 is capture group 1. \2 is capture group 2.

To test your regex and to have an explanation generated, punch your regex and input-string here:

I suggest you use regex101 to truly understand this regex. Type out the input and then slowly type out the regex. Anything that matches the regex is highlighted and color coded.

@Kingswoop 2020-06-30 03:58:43

i have slightly altered the question would this still work?

@Lucius Kaye 2020-06-30 04:01:28

I changed my answer. I edited it and tested it. It's good.

@MrNobody33 2020-06-30 03:38:58

You could try this with re.findall():

import re

st='.      68.00     68.00          .  '



If you have more numbers with different lengths and you only want those with length two, you could try this:

import re

st='.      68      67     .     600 '
print(','.join(re.findall('(?<!\d)(\d{2})(?!\d)',st)))   #you can change \d{2}, to \d{n} with n as the length you want 

See the explanation of the regular expression here.



Another option without using regex:

st='.      68       68     .'
ls=[s for s in st.split() if all(let.isdigit() for let in s)]



@Kingswoop 2020-06-30 03:57:06

I made a slight change to the question would this still work?

@MrNobody33 2020-06-30 04:12:31

See the last edit @Kingswoop!! It will work with that...

@Bruno Portugal 2020-06-30 03:49:24

Alternatively to using regex, the result could also be achieved by the combination of replace, split and join functions:

x = '.      68       68          .'
result = ','.join(x.replace('.','').split())

@Green Cloak Guy 2020-06-30 03:39:12

Regex works for this problem. Here, instead of removing the whitespace, I'm grabbing the numbers, which is equivalent in your example. And then joining them with ','.join(), of course.

>>> import re
>>> inp = '.      68       68          .                                       ' 
>>> print(','.join(re.findall(r'[0-9]+', inp)))

@Kingswoop 2020-06-30 03:58:28

I have slightly altered the question would this still work?

@Green Cloak Guy 2020-06-30 04:42:32

To include the decimal point, you would want to change the search string accordingly. Say, to r'[0-9.]+'

Related Questions

Sponsored Content

45 Answered Questions

[SOLVED] How do I merge two dictionaries in a single expression in Python?

21 Answered Questions

[SOLVED] What are metaclasses in Python?

8 Answered Questions

[SOLVED] Manually raising (throwing) an exception in Python

  • 2010-01-12 21:07:40
  • 1785785 View
  • 2307 Score
  • 8 Answer
  • Tags:   python exception

42 Answered Questions

[SOLVED] How to get the current time in Python

  • 2009-01-06 04:54:23
  • user46646
  • 3339360 View
  • 2941 Score
  • 42 Answer
  • Tags:   python datetime time

13 Answered Questions

[SOLVED] How to remove a key from a Python dictionary?

29 Answered Questions

26 Answered Questions

[SOLVED] Does Python have a ternary conditional operator?

62 Answered Questions

[SOLVED] Calling an external command from Python

10 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

Sponsored Content