regex to extract values within parentheses and the word before parentheses using python -


i extract values within parentheses , word starting before it. example, in given text file, have below details:

total db read time : tdbt(56) tdto(78) tdtc(567) ton(567) tot(345)  

expected output tuple or list:

tdbt 56 tdto 78 tdtc 567 ton 567 tot 345  

split on : , replace () spaces using str.translate

s = "total db read time : tdbt(56) tdto(78) tdtc(567) ton(567) tot(345)"  spl = s.split(":")[1].translate({ord("("):" ",ord(")"):" "}).split() ['tdbt', '56', 'tdto', '78', 'tdtc', '567', 'ton', '567', 'tot', '345'] 

or use str.replace:

s="total db read time : tdbt(56) tdto(78) tdtc(567) ton(567) tot(345)"  spl = s.split(":")[1].replace("("," ").replace(")"," ").split() print(spl) 

if want pairs can use re:

s="total db read time : tdbt(56) tdto(78) tdtc(567) ton(567) tot(345)"  import  re  r = re.compile("(\w+)\((\d+)\)")  print(r.findall(s)) [('tdbt', '56'), ('tdto', '78'), ('tdtc', '567'), ('ton', '567'), ('tot', '345')] 

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -