跳到内容

验证文件数据

pydantic 是一个用于验证来自各种来源数据的强大工具。在本节中,我们将了解如何验证来自不同类型文件的数据。

注意

如果您使用以下任何文件格式来解析配置/设置,您可能需要考虑使用 pydantic-settings 库,该库为解析此类数据提供了内置支持。

JSON 数据

.json 文件是以人类可读格式存储键/值数据的常用方法。这是一个 .json 文件的示例

{
    "name": "John Doe",
    "age": 30,
    "email": "[email protected]"
}

要验证此数据,我们可以使用 pydantic 模型

import pathlib

from pydantic import BaseModel, EmailStr, PositiveInt


class Person(BaseModel):
    name: str
    age: PositiveInt
    email: EmailStr


json_string = pathlib.Path('person.json').read_text()
person = Person.model_validate_json(json_string)
print(repr(person))
#> Person(name='John Doe', age=30, email='[email protected]')

如果文件中的数据无效,pydantic 将引发 ValidationError。假设我们有以下 .json 文件

{
    "age": -30,
    "email": "not-an-email-address"
}

此数据有三个缺陷:1. 缺少 name 字段。2. age 字段为负数。3. email 字段不是有效的电子邮件地址。

当我们尝试验证此数据时,pydantic 会引发一个 ValidationError,其中包含上述所有问题

import pathlib

from pydantic import BaseModel, EmailStr, PositiveInt, ValidationError


class Person(BaseModel):
    name: str
    age: PositiveInt
    email: EmailStr


json_string = pathlib.Path('person.json').read_text()
try:
    person = Person.model_validate_json(json_string)
except ValidationError as err:
    print(err)
    """
    3 validation errors for Person
    name
    Field required [type=missing, input_value={'age': -30, 'email': 'not-an-email-address'}, input_type=dict]
        For further information visit https://errors.pydantic.dev/2.10/v/missing
    age
    Input should be greater than 0 [type=greater_than, input_value=-30, input_type=int]
        For further information visit https://errors.pydantic.dev/2.10/v/greater_than
    email
    value is not a valid email address: An email address must have an @-sign. [type=value_error, input_value='not-an-email-address', input_type=str]
    """

通常,您在一个 .json 文件中拥有大量特定类型的数据。例如,您可能有一个人员列表

[
    {
        "name": "John Doe",
        "age": 30,
        "email": "[email protected]"
    },
    {
        "name": "Jane Doe",
        "age": 25,
        "email": "[email protected]"
    }
]

在这种情况下,您可以根据 list[Person] 模型验证数据

import pathlib

from pydantic import BaseModel, EmailStr, PositiveInt, TypeAdapter


class Person(BaseModel):
    name: str
    age: PositiveInt
    email: EmailStr


person_list_adapter = TypeAdapter(list[Person])  # (1)!

json_string = pathlib.Path('people.json').read_text()
people = person_list_adapter.validate_json(json_string)
print(people)
#> [Person(name='John Doe', age=30, email='[email protected]'), Person(name='Jane Doe', age=25, email='[email protected]')]
  1. 我们使用 TypeAdapter 来验证 Person 对象的列表。TypeAdapter 是 Pydantic 构造,用于针对单个类型验证数据。

JSON 行文件

与从 .json 文件验证对象列表类似,您可以从 .jsonl 文件验证对象列表。.jsonl 文件是由换行符分隔的 JSON 对象序列。

考虑以下 .jsonl 文件

{"name": "John Doe", "age": 30, "email": "[email protected]"}
{"name": "Jane Doe", "age": 25, "email": "[email protected]"}

我们可以使用与用于 .json 文件类似的方法来验证此数据

import pathlib

from pydantic import BaseModel, EmailStr, PositiveInt


class Person(BaseModel):
    name: str
    age: PositiveInt
    email: EmailStr


json_lines = pathlib.Path('people.jsonl').read_text().splitlines()
people = [Person.model_validate_json(line) for line in json_lines]
print(people)
#> [Person(name='John Doe', age=30, email='[email protected]'), Person(name='Jane Doe', age=25, email='[email protected]')]

CSV 文件

CSV 是用于存储表格数据最常见的文件格式之一。要验证来自 CSV 文件的数据,您可以使用 Python 标准库中的 csv 模块加载数据,并根据 Pydantic 模型对其进行验证。

考虑以下 CSV 文件

name,age,email
John Doe,30,[email protected]
Jane Doe,25,[email protected]

以下是我们如何验证该数据

import csv

from pydantic import BaseModel, EmailStr, PositiveInt


class Person(BaseModel):
    name: str
    age: PositiveInt
    email: EmailStr


with open('people.csv') as f:
    reader = csv.DictReader(f)
    people = [Person.model_validate(row) for row in reader]

print(people)
#> [Person(name='John Doe', age=30, email='[email protected]'), Person(name='Jane Doe', age=25, email='[email protected]')]

TOML 文件

TOML 文件通常用于配置,因为它们简单易读。

考虑以下 TOML 文件

name = "John Doe"
age = 30
email = "[email protected]"

以下是我们如何验证该数据

import tomllib

from pydantic import BaseModel, EmailStr, PositiveInt


class Person(BaseModel):
    name: str
    age: PositiveInt
    email: EmailStr


with open('person.toml', 'rb') as f:
    data = tomllib.load(f)

person = Person.model_validate(data)
print(repr(person))
#> Person(name='John Doe', age=30, email='[email protected]')