简体   繁体   English

使用 Python 从 JSON 中获取正确的值

[英]Getting right values from JSON using Python

I have this json file我有这个json文件

https://pastebin.com/embed_js/PknXEGq2 https://pastebin.com/embed_js/PknXEGq2

and here is my code so far到目前为止,这是我的代码

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()

The problem is that I'm not able to get the right values and keys for some, so how I can specify the right elements (Product Name and Price) I want to get from the json file问题是我无法为某些人获得正确的值和键,所以我如何指定我想从 json 文件中获得的正确元素(产品名称和价格)

The result must be结果必须是

You can buy Tommee Tippee Ctn Transition Cup at our store at 12您可以在我们的商店购买 Tommee Tippee Ctn Transition Cup 12

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()

That the first Element and this is the output:第一个元素,这是输出:

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

I rewrote your code.我重写了你的代码。 Please check whether it fulfills your task :请检查它是否满足您的任务:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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