如何让蟒蛇检测逗号?

更新的代码:


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


缥缈止盈
浏览 79回答 2
2回答

jeck猫

您可以使用区域设置模块将价格表示法转换为浮点值。>>> 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():&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; price = locale.atof(limited['BestPrice'])&nbsp; &nbsp; &nbsp; &nbsp; # or&nbsp; &nbsp; &nbsp; &nbsp; # price = float(limited['BestPrice'].replace(',', ''))&nbsp; &nbsp; &nbsp; &nbsp; if price < lowestprice:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; limitedname = limited['Name']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; limitedurl = limited['AbsoluteUrl']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lowestprice = price&nbsp; &nbsp; except ValueError as ve:&nbsp; &nbsp; &nbsp; &nbsp; print(f"Hit a non-numeric value for {limited['Name']} for price: {ve}")print(colored(f"{limitedname}: {lowestprice}\n{limitedurl}\n"))sleep(1)

HUX布斯

添加此示例代码是希望它能帮助您了解正在发生的事情,尽管@Todd肯定涵盖了您需要从多个不同角度执行的操作。import localelocale.setlocale(locale.LC_NUMERIC,'')import requestsfor limited in requests.get('https://search.roblox.com/catalog/json?Catalog'\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Context=1&Keyword=&SortType=0&SortAggregation='\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '3&SortCurrency=0&LegendExpanded=true&Categ'\&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'ory=2&pageNumber=1').json():&nbsp; &nbsp; if limited['BestPrice'] == '':&nbsp; &nbsp; &nbsp; &nbsp; print('This one is empty')&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; print(locale.atof(limited['BestPrice']))结果:4195.06997.02200.08149.04291.02850.03299.01998.023000.03000.014500.010994.03996.01249.03799.06499.0843.03100.01300.0680.02049.02491.04099.02499.02959.010500.0855.08698.02700.03500.019500.05199.08999.055555.02844.02299.05000.01399.0699420.0This one is empty55000.04400.0这些是迭代 json 时出现的最佳价格值。这些是您需要使用的值。其中一个是空字符串。limited
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python