scripting - String splitting python -
i trying parse file , split line in python. can parse file , load list.
i trying take each line , split parenthesis,
'datediff(varchar(30),timestamp(3),timestamp(3))' reverse(text) checkcluster(int,int,int,int,int) declaredown(int)
i trying extract parameters in side function so:
['varchar(30),timestamp(3),timestamp(3)','text','int,int,int,int,int','int']
however result getting is:
['varchar(30', 'text', 'int,int,int,int,int', 'int']
i believe because of bracket in varchar. there way split first occurrence , last occurrence of parenthesis?
is there way split first occurrence , last occurrence of parenthesis?
yes, use partition
, rpartition
.
>>> s = 'datediff(varchar(30),timestamp(3),timestamp(3))' >>> s.partition("(") ('datediff', '(', 'varchar(30),timestamp(3),timestamp(3))') >>> s.partition("(")[2] 'varchar(30),timestamp(3),timestamp(3))' >>> s.partition("(")[2].rpartition(")") ('varchar(30),timestamp(3),timestamp(3)', ')', '') >>> s.partition("(")[2].rpartition(")")[0] 'varchar(30),timestamp(3),timestamp(3)'
>>> def extract(s): ... return s.partition("(")[2].rpartition(")")[0] ... >>> print(extract('datediff(varchar(30),timestamp(3),timestamp(3))')) varchar(30),timestamp(3),timestamp(3) >>> print(extract('reverse(text)')) text >>> print(extract('checkcluster(int,int,int,int,int)')) int,int,int,int,int >>> print(extract('declaredown(int)')) int