1

I have some trouble transforming a string into an array. The separators between numbers are spaces, and the thousands also.

My string is this:

'25 127,4 17 588,6 16 264,3 324,4 8,7'

I want it to look like this:

['25 127,4', '17 588,6', '16 264,3', '324,4', '8,7']

or better like this:

['25127,4', '17588,6', '16264,3', '324,4', '8,7']

I was trying to use regex and findall to do so, but the problem is that it only captures 5-digit numbers.

My code is kind of like this:

a = '25 127,4 17 588,6 16 264,3 324,4 8,7'
print(re.findall(r'\d+\s\d+,\d{1}', a))

which gives me this output:

['25 127,4', '17 588,6', '16 264,3', '4 8,7']

How to solve this?

3
  • 1
    Try (?! )[\d ]+,\d Commented Jul 7, 2021 at 3:06
  • 1
    Maybe re.findall(r'[0-9\s]+,\d+', s) given a string s? Commented Jul 7, 2021 at 3:07
  • Thanks a lot @HaoWu and Mark. It worked!! Commented Jul 7, 2021 at 3:44

2 Answers 2

1

The regex recommendation in the comment seems to work for your example, but having spaces represent delimiters and thousand separators in your data will cause a lot of problems. If possible you should see if a different sort of delimiter could be used!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the recommendations. Sure will change the delimeter
1

You can optionally repeat matching a space and matching 1+ digits. (Note that \s can also match a newline)

Then remove the spaces from the matches.

\b\d+(?: \d+)*,\d\b

Regex demo

import re

pattern = r"\b\d+(?: \d+)*,\d\b"
s = "25 127,4 17 588,6 16 264,3 324,4 8,7"
result = [m.replace(" ", "") for m in re.findall(pattern, s)]
print(result)

Output

['25127,4', '17588,6', '16264,3', '324,4', '8,7']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.