Python Forum
Find number in a text for if statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find number in a text for if statement
#1
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.
Reply
#2
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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")
Reply
#4
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Brick Number stored as text with openpyxl CAD79 2 360 Apr-17-2024, 10:17 AM
Last Post: CAD79
  find the sum of a series of values that equal a number ancorte 1 493 Oct-30-2023, 05:41 AM
Last Post: Gribouillis
  find random numbers that are = to the first 2 number of a list. Frankduc 23 3,188 Apr-05-2023, 07:36 PM
Last Post: Frankduc
  Pypdf2 will not find text standenman 2 932 Feb-03-2023, 10:52 PM
Last Post: standenman
  read a text file, find all integers, append to list oldtrafford 12 3,515 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  find some word in text list file and a bit change to them RolanRoll 3 1,519 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  Find and delete above a certain line in text file cubangt 12 3,453 Mar-18-2022, 07:49 PM
Last Post: snippsat
  How to find tags using specific text (timestamps) in a url? q988988 1 1,369 Mar-08-2022, 08:09 AM
Last Post: buran
  Find if chain of characters or number Frankduc 4 1,795 Feb-11-2022, 01:55 PM
Last Post: Frankduc
  Search text in PDF and output its page number. atomxkai 21 8,896 Jan-21-2022, 06:20 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020