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

javascript - How to process users in one specific order using map o each function? -

java - Two interface have same method name with different return type -

javascript - Linking from page A to a specific iframe on page B -