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'
...
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
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.