简体   繁体   中英

How do I make python detect the commas?

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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM