Python Forum

Full Version: Find number in a text for if statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I have been struggeling to make an if statement based on the condition that a certain number is inside a text.

Text:
{'timestamp': 1531400779308, 'xx': {'xx': 'xx', 'xx': 'xx'}, 'bids': [{'price': 100, 'amount': 1}, {'price': 99, 'amount': 1}]}

So a function that if the price is 99 that the formula continues.

if "99" in text:
print("go")

But i can't really get it right.
Please, post your code in python tags. Any traceback you get - in error tags.
Note that your text looks like json. Does it really have this part xx': {'xx': 'xx', 'xx': 'xx'}?. Or you replaced sensitive information? Also, does it really use single quotes?
the best approach if it is really json is to parse it properly using json module.
As stated by buran, that doesn't look like a string. So I'll help as if it's not a string :)

Try this:
if "bids" in text:
    if any(99 == bid["price"] for bid in text["bids"] if "price" in bid):
        print("go")
    else:
        print("not 99")
else:
    print("invalid text")
(Jul-12-2018, 03:58 PM)nilamo Wrote: [ -> ]As stated by buran, that doesn't look like a string. So I'll help as if it's not a string :)

Try this:
if "bids" in text:
    if any(99 == bid["price"] for bid in text["bids"] if "price" in bid):
        print("go")
    else:
        print("not 99")
else:
    print("invalid text")

Actually I think it is indeed Json data at the source. I didn't really know much about that :(

But your code works actually pretty neat, so thank you!