Reputation: 3311
I have data in python that needs to convert string from
string = "[-8.27104300e-02 9.09485668e-02 7.72242993e-02]"
to this
converted_string = "-8.27104300e-02, 9.09485668e-02, 7.72242993e-02"
Basically removing brackets [] and putting in comma for the spaces
How do I achieve this ?
Upvotes: 0
Views: 57
Reputation: 402563
You don't need regex for removing the braces. That's just a simple slicing operation. However, I could see the need for it when substituting whitespace, especially when you have varying width whitespace separators.
>>> import re
>>> re.sub('\s+', ', ', string[1:-1])
'-8.27104300e-02, 9.09485668e-02, 7.72242993e-02'
However, if you can guarantee a fixed separator width of 2 spaces, then str.replace
works well here -
>>> string[1:-1].replace(' ', ', ')
'-8.27104300e-02, 9.09485668e-02, 7.72242993e-02'
Upvotes: 1