Find number in a text for if statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Find number in a text for if statement (/thread-11507.html) |
Find number in a text for if statement - BitbyBit - Jul-12-2018 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. RE: Find number in a text for if statement - buran - Jul-12-2018 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. RE: Find number in a text for if statement - nilamo - Jul-12-2018 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") RE: Find number in a text for if statement - BitbyBit - Jul-13-2018 (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 :) 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! |