【问题标题】:Getting right values from JSON using Python使用 Python 从 JSON 中获取正确的值
【发布时间】:2020-11-21 12:17:04
【问题描述】:

我有这个 json 文件

https://pastebin.com/embed_js/PknXEGq2

这是我目前的代码

import json

data = json.load(open('data.json'))

Product = []
Products = []

def get_products():
    query_one = data['Bundles']
    for first in query_one:
        Product = first.get('Product') or []
        for i in Product:
            for key, val in i.items():
                if key == None :
                    print(i.index(key) + key)
                elif key is False:
                 print("Error")
                else:
                 print("You can buy " + str(key) + " at our store at " + str(val))
                 
    Products = data['Bundles']
    var_two = Products
    for second in Products:
        var_two = second.get('Products')
        for j in var_two:
            for key_two, val_two in j.items():
                if key_two == None:
                    print(j.index(key_two) + key_two)
                elif key_two is False:
                    print("Error")
                else:
                    print("You can buy " + str(key_two) + " at our store at " + str(val_two))
    return ""

get_products()

问题是我无法为某些获取正确的值和键,所以我如何指定要从 json 文件中获取的正确元素(产品名称和价格)

结果必须是

您可以在 12 点在我们的商店购买 Tommee Tippee Ctn 过渡杯

【问题讨论】:

  • 你能添加一个你需要的实际输出的例子吗?
  • 您可以在我们的商店购买 Tommee Tippee Ctn 过渡杯 12

标签: python arrays json


【解决方案1】:
import json
import sys
import os
data = json.load(open(os.path.dirname(sys.argv[0])+'/file.json'))

Product = []
Products = []

def get_products():
    query_one = data['Bundles']
    
    for first in query_one:
        Product = first.get('Product') or []
        for i in Product:
            name = ""
            price = ""
            for key, val in i.items():
            
                if key == None :
                    pass
                elif key=="Price":
                    price = val
                    
                elif key=="Name":
                    name = val
            print ("You can buy " + str(name) + " at the price  " + str(price))
                

get_products()

第一个元素,这是输出:

You can buy Tommee Tippee Ctn Transition Cup at the price  12

【讨论】:

    【解决方案2】:

    我重写了你的代码。请检查它是否完成您的任务:

    import json
    
    data = json.load(open('data.json'))
    
    ignore_values = ["Size 6-12m & 1-2y", " 6-11kg Girl"]
    
    def get_products():
        bundles = data.get("Bundles")
        for bundle in bundles:
            
            products = bundle.get("Product")
            if products:
                for product in products:
                    product_name = product.get("Name", '')
                    product_price = product.get("Price", '')
                    if any(ignore_value in product_name for ignore_value in ignore_values):
                        for ignore_value in ignore_values:
                            product_name = product_name.replace(ignore_value, '')
                    print("You can buy {0} Ctn Transition Cup at our store at {1}".format(product_name, product_price))
    
            products = bundle.get("Products")
            if products:
                for product in products:
                    product_name = product.get("Name", '')
                    product_price = product.get("Price", '')
                    if any(ignore_value in product_name for ignore_value in ignore_values):
                        for ignore_value in ignore_values:
                            product_name = product_name.replace(ignore_value, '')
                    print("You can buy {0} Ctn Transition Cup at our store at {1}".format(product_name, product_price))
                     
    
                
    get_products()
    

    【讨论】:

    • 这真的很有帮助,谢谢,我还有一个问题是可以从某些产品中删除“Size 6-12m & 1-2y”和“6-11kg Girl”,我的意思是在这些产品的名称
    • 我更新了代码。您可以将需要删除的内容添加到ignore_values 列表中并检查。
    猜你喜欢
    • 2012-09-03
    • 2014-12-21
    • 1970-01-01
    • 2021-06-13
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多