-1

Updated code:

import requests
from time import sleep
import webbrowser
from termcolor import colored
import locale

locale.setlocale(locale.LC_NUMERIC, '')

print(colored('Lowest Priced Limited\n---------------------\n', 'green'))
count = 0
while True:
    lowestprice = 1234567890
    for limited in requests.get('https://search.roblox.com/catalog/json?CatalogContext=1&Keyword=&SortType=0&SortAggregation=3&SortCurrency=0&LegendExpanded=true&Category=2&pageNumber=1').json():
        price = locale.atof(limited['BestPrice'])
        if price < lowestprice:
            limitedname = limited['Name']
            limitedurl = limited['AbsoluteUrl']
            lowestprice = price
    print(colored(f"{limitedname}: {lowestprice}\n{limitedurl}\n"))
    sleep(1)

    if lowestprice <= 220 and count == 0:
        webbrowser.open(limitedurl, new=2)
        count += 1

Derek Eden
  • 4,403
  • 3
  • 18
  • 31
John Rock
  • 11
  • 4
  • You can use the method ```replace(",",".")``` and ```float``` – AlexDotis Apr 06 '20 at 00:03
  • Its better to use it as a currency or you may get into problems by replacing commas or points https://stackoverflow.com/Questions/320929/currency-formatting-in-python – paltaa Apr 06 '20 at 00:04
  • @AlexDotis I think it's supposed to be 3799 not 3.799, so even though it's a hacky and incorrect way of doing things, it should be int not float – Derek Eden Apr 06 '20 at 00:11
  • When I used float instead of int, I got the error ```price = float(limited['BestPrice']) ValueError: could not convert string to float: '3,799' ``` – John Rock Apr 06 '20 at 00:20
  • @DerekEden take a look here.. https://stackoverflow.com/questions/6633523/how-can-i-convert-a-string-with-dot-and-comma-into-a-float-in-python This may answer this question. – AlexDotis Apr 06 '20 at 00:29
  • If you are going to convert the string to int or float you have to replace the , with '' as stated above. Though the locale method is the proper way to do it – Derek Eden Apr 06 '20 at 02:00
  • you can use `lowestprice = sys.maxint` as your initial value. – Todd Apr 07 '20 at 21:18

2 Answers2

2

You can use the locale module to convert price notation into a float value.

>>> import locale
>>> locale.atof("1,000.99")
1000.99

Or if you want integer values only:

>>> locale.atoi("1,000")
1000

Depending on your configuration, you may need:

locale.setlocale(locale.LC_NUMERIC,'')

May also work:

locale.setlocale(locale.LC_ALL, 'en_US.UTF8')

As suggested by others, you can just remove the commas and convert to a float:

price = float(limited['BestPrice'].replace(',', ''))

However, it's better to get familiar with localization resources for a more professional solution.

To skip invalid numeric strings like an empty string:

for limited in requests.get('...').json():
    try:
        price = locale.atof(limited['BestPrice'])
        # or
        # price = float(limited['BestPrice'].replace(',', ''))
        if price < lowestprice:
            limitedname = limited['Name']
            limitedurl = limited['AbsoluteUrl']
            lowestprice = price

    except ValueError as ve:
        print(f"Hit a non-numeric value for {limited['Name']} for price: {ve}")

print(colored(f"{limitedname}: {lowestprice}\n{limitedurl}\n"))
sleep(1)
Todd
  • 4,669
  • 1
  • 22
  • 30
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/211038/discussion-on-answer-by-todd-how-do-i-make-python-detect-the-commas). – Samuel Liew Apr 06 '20 at 02:54
0

Added this sample code in the hopes that it helps you understand what's going on, though @Todd has definitely covered what you need to do from multiple different angles.

import locale
locale.setlocale(locale.LC_NUMERIC,'')
import requests
for limited in requests.get('https://search.roblox.com/catalog/json?Catalog'\
                            'Context=1&Keyword=&SortType=0&SortAggregation='\
                                '3&SortCurrency=0&LegendExpanded=true&Categ'\
                                    'ory=2&pageNumber=1').json():
    if limited['BestPrice'] == '':
        print('This one is empty')
    else:
        print(locale.atof(limited['BestPrice']))

result:

4195.0
6997.0
2200.0
8149.0
4291.0
2850.0
3299.0
1998.0
23000.0
3000.0
14500.0
10994.0
3996.0
1249.0
3799.0
6499.0
843.0
3100.0
1300.0
680.0
2049.0
2491.0
4099.0
2499.0
2959.0
10500.0
855.0
8698.0
2700.0
3500.0
19500.0
5199.0
8999.0
55555.0
2844.0
2299.0
5000.0
1399.0
699420.0
This one is empty
55000.0
4400.0

These are the bestprice values that are occurring in limited as you iterate over the json. These are the values you need to work with. One of them is an empty string.

Derek Eden
  • 4,403
  • 3
  • 18
  • 31