3

I am trying to convert a string of words and numbers into a list, every item is separated with a space, so using .replace(" ", ",").split(",") would be an easy solution, but unfortunately, sometimes there are multiple words in the object name, and I would like these words to be connected with a _

Example:

office supplies 674.56 570.980487 755.84 682.360029

Expected output:

office_supplies 674.56 570.980487 755.84 682.360029

I have found this: Replace spaces between letters only

And tried to implement it like this:

sample_line = "office supplies 674.56 570.980487 755.84 682.360029"
regex = re.compile(':%s/\v(\a)\s(\a)/\1_\2/g', re.I)
print(re.sub(p, r"\1\2", line))

But it does not seem to replace the spaces, I am not very sharp with regex, but according to the linked issue, it should work.

2
  • What you've found i vim solution not python Commented Jul 4, 2022 at 7:05
  • \D\s\D should look for whitespaces between non-digit characters regex101.com/r/t0kOKw/1 Commented Jul 4, 2022 at 7:11

2 Answers 2

4

You may probably use this re.sub + split solution:

import re
s = 'office supplies 674.56 570.980487 755.84 682.360029'
print ( re.sub(r'(?<=[a-zA-Z])\s+(?=[a-zA-Z])', '_', s).split() )

Output:

['office_supplies', '674.56', '570.980487', '755.84', '682.360029']

Here:

  • Regex (?<=[a-zA-Z])\s+(?=[a-zA-Z]) matches 1+ whitespace surrounded with letters only
  • split will split string on whitespaces
Sign up to request clarification or add additional context in comments.

Comments

1
x=r'office supplies 674.56 570.980487 755.84 682.360029'
lead="_".join(x.split()[:2])
trail=" ".join(x.split()[2:])

expected_string = lead + " " + trail
print(expected_string)

1 Comment

Thanks, but the issue got solved already 👍🏼

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.