Axil
Axil

Reputation: 3311

python - regex - delimit array string with comma nicely

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.

Upvotes: 0

Views: 57

Answers (2)

Tim
Tim

Reputation: 2637

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

Upvotes: 1

kantal
kantal

Reputation: 2407

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("[] "))

Upvotes: 0

Related Questions