【问题标题】:Python JSON find specific dataPython JSON 查找特定数据
【发布时间】:2020-08-23 13:32:52
【问题描述】:

我有这个 JSON 文件结构(从网站解析的销售产品)。 JSON 的一部分:

{
 "shopName": "Shop",
 "promotions": [
  {
   "productName": "Cookies",
   "oldPrice": 11.99,
   "newPrice": 7.99,
   "discount": 33
  },
  {
   "productName": "Butter",
   "oldPrice": 27.15,
   "newPrice": 21.99,
   "discount": 19
  },
  {
   "productName": "Milk",
   "oldPrice": 30.45,
   "newPrice": 21.99,
   "discount": 27
  }
 ]
}

问题是如何仅显示比给定数量更大的折扣的产品(具有所有功能:名称、旧价格、新价格、折扣)。

【问题讨论】:

    标签: python json web-scraping


    【解决方案1】:

    这应该可行:

    data = {
     "shopName": "Shop",
     "promotions": [
      {
       "productName": "Cookies",
       "oldPrice": 11.99,
       "newPrice": 7.99,
       "discount": 33
      },
      {
       "productName": "Butter",
       "oldPrice": 27.15,
       "newPrice": 21.99,
       "discount": 19
      },
      {
       "productName": "Milk",
       "oldPrice": 30.45,
       "newPrice": 21.99,
       "discount": 27
      }
     ]
    }
    
    MIN_PRICE = 20
    
    filtered_products = [p for p in data['promotions'] if p['discount'] >= MIN_PRICE]
    
    print(filtered_products)
    
    

    这打印:

    [
      {
       "productName": "Cookies",
       "oldPrice": 11.99,
       "newPrice": 7.99,
       "discount": 33
      },
      {
       "productName": "Milk",
       "oldPrice": 30.45,
       "newPrice": 21.99,
       "discount": 27
      }
    ]
    

    另一种方法是使用filter函数:

    filtered_products = list(filter(lambda p: p['discount'] > MIN_PRICE, data['promotions']))
    

    【讨论】:

      【解决方案2】:

      试试这个。

      import json, pandas as pd
      
      df=pd.DataFrame(json.loads('{ "shopName": "Shop", "promotions": [  {   "productName": "Cookies",   "oldPrice": 11.99,   "newPrice": 7.99,   "discount": 33  },  {   "productName": "Butter",   "oldPrice": 27.15,   "newPrice": 21.99,   "discount": 19  },  {   "productName": "Milk",   "oldPrice": 30.45,   "newPrice": 21.99,   "discount":27  } ]}')['promotions'])
      
      print(df)
      
        productName  oldPrice  newPrice  discount
      0     Cookies     11.99      7.99        33
      1      Butter     27.15     21.99        19
      2        Milk     30.45     21.99        27
      
      print(df[df.discount==df.discount.max()])
      
       productName  oldPrice  newPrice  discount
      0     Cookies     11.99      7.99        33
      

      【讨论】:

        【解决方案3】:

        如果你已经解析过 JSON,你可以使用这个

        from typing import Dict, Any
        
        parsed_json = {
        "shopName": "Shop",
        "promotions": [
            {"productName": "Cookies", "oldPrice": 11.99, "newPrice": 7.99, "discount": 33},
            {"productName": "Butter", "oldPrice": 27.15, "newPrice": 21.99, "discount": 19},
            {"productName": "Milk", "oldPrice": 30.45, "newPrice": 21.99, "discount": 27},
            ],
        }
        
        
        def find_specific_data(num: int, data: Dict[Any, Any]) -> Dict[Any, Any]:
            for value in data["promotions"]:
                if value["discount"] > num:
                    print(value)
        
        find_specific_data(26, parsed_json)
        
        In: find_specific_data(26)
        Out: {'productName': 'Cookies', 'oldPrice': 11.99, 'newPrice': 7.99, 'discount': 33}
             {'productName': 'Milk', 'oldPrice': 30.45, 'newPrice': 21.99, 'discount': 27}
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-06-08
          • 2018-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多