Python列表到嵌套Json
我有以下问题Python列表到嵌套Json,python,json,jupyter-notebook,Python,Json,Jupyter Notebook,我有以下问题 class Inventory: def __init__(self,project_no,country,category,product,count): self.project_no = project_no self.country = country self.category = category self.product = product self.count = count i
class Inventory:
def __init__(self,project_no,country,category,product,count):
self.project_no = project_no
self.country = country
self.category = category
self.product = product
self.count = count
inventory_list = []
inventory_list.append(Inventory(1,'USA','Beverages','Milk',2))
inventory_list.append(Inventory(1,'USA','Beverages','Juice',5))
inventory_list.append(Inventory(1,'USA','Snacks','Potato Chips',2))
inventory_list.append(Inventory(1,'USA','Oils','Canola',5))
inventory_list.append(Inventory(1,'USA','Oils','Olive',8))
inventory_list.append(Inventory(1,'CAN','Beverages','Milk',7))
inventory_list.append(Inventory(1,'CAN','Beverages','Juice',8))
inventory_list.append(Inventory(1,'CAN','Snacks','Potato Chips',8))
inventory_list.append(Inventory(1,'CAN','Oils','Canola',3))
inventory_list.append(Inventory(1,'CAN','Oils','Olive',4))
{'Inventory': [{'Country': inv.country , 'Category' : [{inv.category : [{'Product' : inv.product}]}] } for inv in inventory_list]}
这段代码给出了以下输出
我真正需要的是下面的内容
如何做到这一点?
我认为列表理解是最好的方法。
但我遇到的麻烦超出了这一点。
我认为这对于python程序员来说应该很容易。
用我有限的python,我只能做到这一点。
如果有人能提供帮助。尝试使用
json
模块,例如
import json
...
inv_json = {'Inventory': [{'Country': inv.country , 'Category' : [{inv.category : [{'Product' : inv.product}]}] } for inv in inventory_list]}
json_formatted_str = json.dumps(x, indent=2)
print(json_formatted_str)
尝试使用
json
模块,例如
import json
...
inv_json = {'Inventory': [{'Country': inv.country , 'Category' : [{inv.category : [{'Product' : inv.product}]}] } for inv in inventory_list]}
json_formatted_str = json.dumps(x, indent=2)
print(json_formatted_str)
我建议您尝试使用模块序列化您的清单类。不过,看起来您需要稍微重新组织一下数据。据我所知,您希望有一个库存,其中包含一组产品,这些产品分为类别
json.dumps(inv, indent=2, cls=MyEncoder)
首先,让我们定义产品类:
class Product(object):
def __init__(self, name, count):
self.product = name
self.count = count
接下来,我们可以将国家类定义为一组产品的容器,在字典中使用类别名称作为键进行排列
class Country(object):
def __init__(self, name):
self.name = name
self.categories = dict()
def add_product_to_category(self, category, product):
if category not in self.categories:
self.categories[category] = []
self.categories[category].append(product)
然后,我们可以将Inventory类重新定义为一组Country对象的容器
class Inventory(object):
def __init__(self, project_no):
self.project_no = project_no
self.countries = []
接下来,我们可以使用简单的方法用所需的数据填充类
inv = Inventory(1)
us_set = Country('USA')
us_set.add_product_to_category('Beverages', Product('Milk', 2))
us_set.add_product_to_category('Beverages', Product('Juice', 5))
us_set.add_product_to_category('Snacks', Product('Potato Chips', 2))
us_set.add_product_to_category('Oils', Product('Canola', 5))
us_set.add_product_to_category('Oils', Product('Olive', 8))
canada_set = Country('CAN')
canada_set.add_product_to_category('Beverages', Product('Milk', 7))
canada_set.add_product_to_category('Beverages', Product('Juice', 8))
canada_set.add_product_to_category('Snacks', Product('Potato Chips', 8))
canada_set.add_product_to_category('Oils', Product('Canola', 3))
canada_set.add_product_to_category('Oils', Product('Olive', 4))
inv.countries.append(us_set)
inv.countries.append(canada_set)
最后,(为了真正回答您的问题,lul)要序列化Inventory类,我们必须定义一个编码器来使用:
class MyEncoder(json.JSONEncoder):
def default(self, o):
return o.__dict__
现在,我们可以调用json.dumps()来获取序列化的清单的字符串输出
json.dumps(inv, indent=2, cls=MyEncoder)
输出不完全是您所设计的,但是我认为这种方法对您来说会很好
{
"project_no": 1,
"countries": [
{
"name": "USA",
"categories": {
"Beverages": [
{
"count": 2,
"product": "Milk"
},
{
"count": 5,
"product": "Juice"
}
],
"Oils": [
{
"count": 5,
"product": "Canola"
},
{
"count": 8,
"product": "Olive"
}
],
"Snacks": [
{
"count": 2,
"product": "Potato Chips"
}
]
}
},
{
"name": "CAN",
"categories": {
"Beverages": [
{
"count": 7,
"product": "Milk"
},
{
"count": 8,
"product": "Juice"
}
],
"Oils": [
{
"count": 3,
"product": "Canola"
},
{
"count": 4,
"product": "Olive"
}
],
"Snacks": [
{
"count": 8,
"product": "Potato Chips"
}
]
}
}
]
}
我建议您尝试使用模块序列化清单类。不过,看起来您需要稍微重新组织一下数据。据我所知,您希望有一个库存,其中包含一组产品,这些产品分为类别
json.dumps(inv, indent=2, cls=MyEncoder)
首先,让我们定义产品类:
class Product(object):
def __init__(self, name, count):
self.product = name
self.count = count
接下来,我们可以将国家类定义为一组产品的容器,在字典中使用类别名称作为键进行排列
class Country(object):
def __init__(self, name):
self.name = name
self.categories = dict()
def add_product_to_category(self, category, product):
if category not in self.categories:
self.categories[category] = []
self.categories[category].append(product)
然后,我们可以将Inventory类重新定义为一组Country对象的容器
class Inventory(object):
def __init__(self, project_no):
self.project_no = project_no
self.countries = []
接下来,我们可以使用简单的方法用所需的数据填充类
inv = Inventory(1)
us_set = Country('USA')
us_set.add_product_to_category('Beverages', Product('Milk', 2))
us_set.add_product_to_category('Beverages', Product('Juice', 5))
us_set.add_product_to_category('Snacks', Product('Potato Chips', 2))
us_set.add_product_to_category('Oils', Product('Canola', 5))
us_set.add_product_to_category('Oils', Product('Olive', 8))
canada_set = Country('CAN')
canada_set.add_product_to_category('Beverages', Product('Milk', 7))
canada_set.add_product_to_category('Beverages', Product('Juice', 8))
canada_set.add_product_to_category('Snacks', Product('Potato Chips', 8))
canada_set.add_product_to_category('Oils', Product('Canola', 3))
canada_set.add_product_to_category('Oils', Product('Olive', 4))
inv.countries.append(us_set)
inv.countries.append(canada_set)
最后,(为了真正回答您的问题,lul)要序列化Inventory类,我们必须定义一个编码器来使用:
class MyEncoder(json.JSONEncoder):
def default(self, o):
return o.__dict__
现在,我们可以调用json.dumps()来获取序列化的清单的字符串输出
json.dumps(inv, indent=2, cls=MyEncoder)
输出不完全是您所设计的,但是我认为这种方法对您来说会很好
{
"project_no": 1,
"countries": [
{
"name": "USA",
"categories": {
"Beverages": [
{
"count": 2,
"product": "Milk"
},
{
"count": 5,
"product": "Juice"
}
],
"Oils": [
{
"count": 5,
"product": "Canola"
},
{
"count": 8,
"product": "Olive"
}
],
"Snacks": [
{
"count": 2,
"product": "Potato Chips"
}
]
}
},
{
"name": "CAN",
"categories": {
"Beverages": [
{
"count": 7,
"product": "Milk"
},
{
"count": 8,
"product": "Juice"
}
],
"Oils": [
{
"count": 3,
"product": "Canola"
},
{
"count": 4,
"product": "Olive"
}
],
"Snacks": [
{
"count": 8,
"product": "Potato Chips"
}
]
}
}
]
}
代码是如何给出输出的?代码没有输出任何内容。检查或询问更多相关详细信息。@CristiFati代码肯定会输出一些东西,至少在IPython控制台中是这样。@CristiFati我正在使用Jupyter笔记本。我得到输出。如果您将
dict
转换为JSON字符串,并且可以使用内置的JSON
包进行漂亮的打印@对不起,我面临的问题不是输出的美观,而是实际结构本身。代码是如何给出输出的?代码没有输出任何内容。检查或询问更多相关详细信息。@CristiFati代码肯定会输出一些东西,至少在IPython控制台中是这样。@CristiFati我正在使用Jupyter笔记本。我得到输出。如果您将dict
转换为JSON字符串,并且可以使用内置的JSON
包进行漂亮的打印@抱歉,我面临的问题不是输出的美观,而是实际结构本身。问题不是对json进行美观。json本身的内容需要做一些工作。您希望修复哪些内容?您希望看到什么是您没有看到的?例如,“国家”:“美国”在我的输出中重复出现。但是在期望的输出中,它只发生一次。啊,我明白了。我认为james trickington的答案就足够了,在这种情况下,问题不在于对json进行修饰。json本身的内容需要做一些工作。您希望修复哪些内容?您希望看到什么是您没有看到的?例如,“国家”:“美国”在我的输出中重复出现。但是在期望的输出中,它只发生一次。啊,我明白了。我认为詹姆斯·特里金顿的回答在这种情况下就足够了