Bil*_*ong 9 python json pydantic
我有一个工作模型可以json使用pydantic. 模型数据集如下所示:
data = {'thing_number': 123,
'thing_description': 'duck',
'thing_amount': 4.56}
Run Code Online (Sandbox Code Playgroud)
我想做的是将json文件列表作为数据集并能够验证它们。最终,该列表将转换为记录以pandas供进一步处理。我的目标是验证json看起来像这样的任意长的条目列表:
bigger_data = [{'thing_number': 123,
'thing_description': 'duck',
'thing_amount': 4.56},
{'thing_number': 456,
'thing_description': 'cow',
'thing_amount': 7.89}]
Run Code Online (Sandbox Code Playgroud)
我现在的基本设置如下。请注意,添加class ItemList是尝试使任意长度起作用的一部分。
from typing import List
from pydantic import BaseModel
from pydantic.schema import schema
import json
class Item(BaseModel):
thing_number: int
thing_description: str
thing_amount: float
class ItemList(BaseModel):
each_item: List[Item]
Run Code Online (Sandbox Code Playgroud)
然后,基本代码将生成我认为我在一个将接受Item对象的数组对象中寻找的内容。
item_schema = schema([ItemList])
print(json.dumps(item_schema, indent=2))
{
"definitions": {
"Item": {
"title": "Item",
"type": "object",
"properties": {
"thing_number": {
"title": "Thing_Number",
"type": "integer"
},
"thing_description": {
"title": "Thing_Description",
"type": "string"
},
"thing_amount": {
"title": "Thing_Amount",
"type": "number"
}
},
"required": [
"thing_number",
"thing_description",
"thing_amount"
]
},
"ItemList": {
"title": "ItemList",
"type": "object",
"properties": {
"each_item": {
"title": "Each_Item",
"type": "array",
"items": {
"$ref": "#/definitions/Item"
}
}
},
"required": [
"each_item"
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
该设置适用于正在传递的单个 json 项目:
item = Item(**data)
print(item)
Item thing_number=123 thing_description='duck' thing_amount=4.56
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将单个项目传递给ItemList模型时,它返回一个错误:
item_list = ItemList(**data)
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
<ipython-input-94-48efd56e7b6c> in <module>
----> 1 item_list = ItemList(**data)
/opt/conda/lib/python3.7/site-packages/pydantic/main.cpython-37m-x86_64-linux-gnu.so in pydantic.main.BaseModel.__init__()
/opt/conda/lib/python3.7/site-packages/pydantic/main.cpython-37m-x86_64-linux-gnu.so in pydantic.main.validate_model()
ValidationError: 1 validation error for ItemList
each_item
field required (type=value_error.missing)
Run Code Online (Sandbox Code Playgroud)
我也试过传入bigger_data数组,认为它需要以列表的形式开始。这也会返回一个错误——虽然,我至少对字典错误有了更好的理解,但我不知道如何解决。
item_list2 = ItemList(**data_big)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-100-8fe9a5414bd6> in <module>
----> 1 item_list2 = ItemList(**data_big)
TypeError: MetaModel object argument after ** must be a mapping, not list
Run Code Online (Sandbox Code Playgroud)
谢谢。
我尝试过的其他事情
我试过将数据传递给特定的键,运气好一点(也许?)。
item_list2 = ItemList(each_item=data_big)
---------------------------------------------------------------------------
ValidationError Traceback (most recent call last)
<ipython-input-111-07e5c12bf8b4> in <module>
----> 1 item_list2 = ItemList(each_item=data_big)
/opt/conda/lib/python3.7/site-packages/pydantic/main.cpython-37m-x86_64-linux-gnu.so in pydantic.main.BaseModel.__init__()
/opt/conda/lib/python3.7/site-packages/pydantic/main.cpython-37m-x86_64-linux-gnu.so in pydantic.main.validate_model()
ValidationError: 6 validation errors for ItemList
each_item -> 0 -> thing_number
field required (type=value_error.missing)
each_item -> 0 -> thing_description
field required (type=value_error.missing)
each_item -> 0 -> thing_amount
field required (type=value_error.missing)
each_item -> 1 -> thing_number
field required (type=value_error.missing)
each_item -> 1 -> thing_description
field required (type=value_error.missing)
each_item -> 1 -> thing_amount
field required (type=value_error.missing)
Run Code Online (Sandbox Code Playgroud)
eri*_*cbn 26
以下方法也适用,并且不需要根类型。
要从 a 转换List[dict]为 a List[Item]:
items = parse_obj_as(List[Item], bigger_data)
Run Code Online (Sandbox Code Playgroud)
要将 JSON 转换str为List[Item]:
items = parse_raw_as(List[Item], bigger_data_json)
Run Code Online (Sandbox Code Playgroud)
要将 a 转换List[Item]为 JSON str:
from pydantic.json import pydantic_encoder
bigger_data_json = json.dumps(items, default=pydantic_encoder)
Run Code Online (Sandbox Code Playgroud)
或使用自定义编码器:
from pydantic.json import pydantic_encoder
def custom_encoder(**kwargs):
def base_encoder(obj):
if isinstance(obj, BaseModel):
return obj.dict(**kwargs)
else:
return pydantic_encoder(obj)
return base_encoder
bigger_data_json = json.dumps(items, default=custom_encoder(by_alias=True))
Run Code Online (Sandbox Code Playgroud)
oli*_*bre 10
为了避免"each_item"在 中ItemList,您可以使用__root__Pydantic 关键字:
from typing import List
from pydantic import BaseModel
class Item(BaseModel):
thing_number: int
thing_description: str
thing_amount: float
class ItemList(BaseModel):
__root__: List[Item] # ?-- __root__
Run Code Online (Sandbox Code Playgroud)
要构建item_list:
from typing import List
from pydantic import BaseModel
class Item(BaseModel):
thing_number: int
thing_description: str
thing_amount: float
class ItemList(BaseModel):
__root__: List[Item] # ?-- __root__
Run Code Online (Sandbox Code Playgroud)
支持 Pydantic 的网络框架通常 jsonify,例如ItemList没有中间__root__关键字的 JSON 数组。
from typing import List
from pydantic import BaseModel
import json
class Item(BaseModel):
thing_number: int
thing_description: str
thing_amount: float
class ItemList(BaseModel):
each_item: List[Item]
Run Code Online (Sandbox Code Playgroud)
基于您的代码,将 each_item 作为项目列表
a_duck = Item(thing_number=123, thing_description="duck", thing_amount=4.56)
print(a_duck.json())
a_list = ItemList(each_item=[a_duck])
print(a_list.json())
Run Code Online (Sandbox Code Playgroud)
生成以下输出:
{"thing_number": 123, "thing_description": "duck", "thing_amount": 4.56}
{"each_item": [{"thing_number": 123, "thing_description": "duck", "thing_amount": 4.56}]}
Run Code Online (Sandbox Code Playgroud)
使用这些作为“条目 json”:
a_json_duck = {"thing_number": 123, "thing_description": "duck", "thing_amount": 4.56}
a_json_list = {
"each_item": [
{"thing_number": 123, "thing_description": "duck", "thing_amount": 4.56}
]
}
print(Item(**a_json_duck))
print(ItemList(**a_json_list))
Run Code Online (Sandbox Code Playgroud)
工作正常并生成:
Item thing_number=123 thing_description='duck' thing_amount=4.56
ItemList each_item=[<Item thing_number=123 thing_description='duck' thing_amount=4.56>]
Run Code Online (Sandbox Code Playgroud)
我们只剩下唯一的数据:
just_datas = [
{"thing_number": 123, "thing_description": "duck", "thing_amount": 4.56},
{"thing_number": 456, "thing_description": "cow", "thing_amount": 7.89},
]
item_list = ItemList(each_item=just_datas)
print(item_list)
print(type(item_list.each_item[1]))
print(item_list.each_item[1])
Run Code Online (Sandbox Code Playgroud)
这些按预期工作:
ItemList each_item=[<Item thing_number=123 thing_description='duck'thing_amount=4.56>,<Item thin…
<class '__main__.Item'>
Item thing_number=456 thing_description='cow' thing_amount=7.89
Run Code Online (Sandbox Code Playgroud)
因此,如果我遗漏了某些东西,pydantic 库会按预期工作。
我的pydantic版本:0.30 python 3.7.4
从相似文件中读取:
json_data_file = """[
{"thing_number": 123, "thing_description": "duck", "thing_amount": 4.56},
{"thing_number": 456, "thing_description": "cow", "thing_amount": 7.89}]"""
from io import StringIO
item_list2 = ItemList(each_item=json.load(StringIO(json_data_file)))
Run Code Online (Sandbox Code Playgroud)
工作也不错。