0

I am using this to decode delimit array string with comma:

formatted_string = re.sub('\s+', ', ', unknown_encoding_string[1:-1])

seems to work with this (noticed it still has comma behind but anyway it works)

unknown_encoding_string = "[-0.03833389  0.00832078  0.1206817   0.01020864 
 0.01418733  0.01334922  0.0180524 ]"

formatted_string = "-0.03833389, 0.00832078, 0.1206817, 0.01020864, 0.01418733, 0.01334922, 0.0180524,"

eg: https://pastebin.com/eSVj1K6Q

but not with this. in the front it has " ," which cause a problem.

unknown_encoding_string = "[ -0.03833389  0.00832078 -5.50815463e-02
2.86253393e-02 -1.66405290e-02  2.03181207e-02]"

formatted_string = ", -0.03833389, 0.00832078, -5.50815463e-02, 2.86253393e-02, -1.66405290e-02, 2.03181207e-02"

eg: https://pastebin.com/UjswSVSs

I want it delimited if possible nicely like this

"123,4342,54534"

Im using Python for this.

4
  • You could first strip it unknown_encoding_string[1:-1].strip() Commented Jul 12, 2019 at 1:22
  • what does strip does ? Commented Jul 12, 2019 at 1:24
  • it works by the way! thanks Commented Jul 12, 2019 at 1:28
  • By default, it strips all blank spaces on both sides of the string. Commented Jul 12, 2019 at 1:43

2 Answers 2

1

Python has many great tools for doing manipulating strings without needing to resort to regular expressions.

unknown_encoding_string = "[-0.03833389  0.00832078  0.1206817   0.01020864   0.01418733  0.01334922  0.0180524 ]"

# Strip removes the specified characters from the start and end of the string
cleaned_string = unknown_encoding_string.strip("[] ")

# Split converts your string into a list of strings; by default splits on space
values = cleaned_string.split()

# Join will take an iterable and join it with the specified string as the joining character
formatted_string = ",".join(values)

# or in a single line...
formatted_string = ",".join(unknown_encoding_string.strip("[] ").split())

Hope that helps

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

Comments

0

With regex, you can insert a comma between two non-space characters:

re.sub(r"(\S)\s+(\S)",r"\1, \2",text)

You can combine it with strip():

re.sub(r"(\S)\s+(\S)",r"\1, \2",text.strip("[] "))

\1,\2 equal to matched character in group 1 and 2 in parentheses.

Or we can use look-behind and look-ahead:

re.sub(r"(?<=\S)\s+(?=\S)",r", ",text.strip("[] "))

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.