Title: Removing characters/whitespace up to the first chosen character python
Tags: python
Question: 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:
68.00,68.00
Have tried strip and some initial character removal functions but having issues e.g.
Any help would be appreciated.
Comment: What happens if there be more than just two numbers in the input?


Here is another answer: 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          .     '
print(the_input)
# 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)
print(the_output)
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:
https://regex101.com/
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.
Comment for this answer: i have slightly altered the question would this still work?
Comment for this answer: I changed my answer. I edited it and tested it. It's good.

Here is another answer: 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.
&gt;&gt;&gt; import re
&gt;&gt;&gt; inp = '.      68       68          .                                       '
&gt;&gt;&gt; print(','.join(re.findall(r'[0-9]+', inp)))
'68,68'
Comment for this answer: I have slightly altered the question would this still work?
Comment for this answer: To include the decimal point, you would want to change the search string accordingly. Say, to `r'[0-9.]+'`

Here is another answer: You could try this with re.findall():
import re
st='.      68.00     68.00          .  '
print(','.join(re.findall('(?<!\d)(\d{2}.00)(?!\d)',st)))
Output:
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.
Output:
Another option without using regex:
st='.      68       68     .'
ls=[s for s in st.split() if all(let.isdigit() for let in s)]
print(','.join(ls))
Output:
Comment for this answer: I made a slight change to the question would this still work?
Comment for this answer: See the last edit @Kingswoop!! It will work with that...

Here is another answer: 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())

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:06/30/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python>', 'Title': 'Removing characters/whitespace up to the first chosen character python', 'ViewCount': 66, 'id': 62649886, 'user_AboutMe': None, 'user_AccountId': 17352215.0, 'user_DisplayName': 'Kingswoop', 'user_Reputation': 149.0, 'user_Views': 29.0, 'user_id': 12570467.0}}