如何让python检测逗号?

如何让python检测逗号?,python,Python,更新代码: 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: l

更新代码:

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


您可以使用locale模块将价格符号转换为浮点值

>>> import locale
>>> locale.atof("1,000.99")
1000.99
或者,如果只需要整数值:

>>> locale.atoi("1,000")
1000
根据您的配置,您可能需要:

locale.setlocale(locale.LC_NUMERIC,'')
也可用于:

locale.setlocale(locale.LC_ALL, 'en_US.UTF8')
正如其他人所建议的,您只需删除逗号并转换为浮点:

price = float(limited['BestPrice'].replace(',', ''))
但是,最好熟悉本地化资源,以获得更专业的解决方案

要跳过无效的数字字符串(如空字符串),请执行以下操作:

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肯定从多个不同角度介绍了您需要做的事情

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']))
结果:

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

当您在json上迭代时,这些是在有限范围内出现的bestprice值。这些是您需要使用的值。其中一个是空字符串。

您可以使用方法replace、、,,。浮动将其用作一种货币更好,否则你可能会因为替换逗号或点而陷入问题@AlexDotis我认为它应该是3799而不是3.799,因此,尽管这是一种黑客和不正确的操作方式,但它应该是int而不是float当我使用float而不是int时,我得到了错误price=floatlimited['BestPrice']ValueError:无法将字符串转换为float:'3799'@DerekEden在此处查看。。这可能回答了这个问题。评论不是为了进一步讨论;这段对话已经结束。