Home > Back-end >  I want to extract a specific value from a string in python
I want to extract a specific value from a string in python

Time:01-27

I have a string from which I want to extract value for test1. The string is :

I_ID [I_ITEM = [I_ITEM [test1 = F135], I_ITEM [test1 = W1972544]]]]]

Any pointers will be helpfull

CodePudding user response:

Assuming you want to capture all values whose keys are test1, we can try using re.findall:

inp = "I_ID [I_ITEM = [I_ITEM [test1 = F135], I_ITEM [test1 = W1972544]]]]]"
values = re.findall(r'\btest1\s*=\s*(.*?)\]', inp)
print(values)  # ['F135', 'W1972544']
  •  Tags:  
  • Related