Reputation: 361
My string is like below:
Str=S1('amm','string'),S2('amm_sec','string'),S3('amm_','string')
How can I Split the string so that my str_list item becomes:
Str_List[0]=S1('amm','string')
Str_List[1]=S2('amm_sec','string')
...
If I use Str.split(',')
then the output is:
Str_List[0]=S1('amm'
...
Upvotes: 0
Views: 418
Reputation: 1907
you can use regex with re
in python
import re
Str = "S1('amm','string'),S2('amm_sec','string'),S3('amm_','string')"
lst = re.findall("S\d\(.*?\)", Str)
this will give you:
["S1('amm','string')", "S2('amm_sec','string')", "S3('amm_','string')"]
to explain the regex a little more:
S
first you match 'S'
\d
next look for a digit
\(
then the '(' character
.*?
with any number of characters in the middle (but match as few as you can)
\)
followed by the last ')' character
you can play with the regex a little more here
Upvotes: 1
Reputation: 724
My first thought would be to replace ',S'
with ' S'
using regex and split on spaces.
import re
Str = re.sub(',S',' S',Str)
Str_list = Str.split()
Upvotes: 0