If I have a few strings:
string1 = 'text here pri=1 more text urgent=True'
string2 = 'more text here pri=5 urgent=False'
string3 = 'another text pri=3 urgent=False'
string4 = 'more text pri=10 urgent=True'
how can I grab the value of "pri" and "urgent" from the string to a variable?
I came up with a few solutions like for pri just locating it using str.find and then jumping 4 variables ahead, etc....
but there must be a more efficient way to read it, how can I do so?
-
1What did you try? Please, spend some time reading "How to create a Minimal, Complete, and Verifiable example" and "How do I ask a good question?". You will get better results by following the tips in those articles.accdias– accdias2022年03月11日 17:13:04 +00:00Commented Mar 11, 2022 at 17:13
-
Look into regular expressionsRobert– Robert2022年03月11日 18:59:17 +00:00Commented Mar 11, 2022 at 18:59
1 Answer 1
you can just use the .split() function to get all the different words. It would return a list. For string 1:
['text', 'here', 'pri=1', 'more', 'text', 'urgent=True']
so now you can loop through the list and search for '='.
Sign up to request clarification or add additional context in comments.
Comments
lang-py