根据你的操作系统执行对应命令,直接复制运行即可:
在终端执行以下命令,用于后续的元数据格式校验:
pip install jsonschema
工具安装完成后,新建3个文件:metadata_schema.json(校验规则)、process_archives.py(处理脚本)、old_archives.csv(旧档案数据)
标准化的核心是固定必填字段,禁止自由增减,以下为通用必选字段:
重要规则
使用官方标准JSON Schema定义校验规则,将以下代码完整复制到metadata_schema.json文件中,无需修改:
{
"$schema": "http://json-schema.org/draft-07/schema",
"title": "StandardArchiveMetadata",
"type": "object",
"required": ["arch_id", "arch_type", "create_time", "creator", "file_size", "storage_path", "access_level"],
"properties": {
"arch_id": {"type": "string", "format": "uuid"},
"arch_type": {"type": "string", "enum": ["document", "image", "audio", "video", "other"]},
"create_time": {"type": "string", "format": "date-time"},
"creator": {"type": "string"},
"file_size": {"type": "integer", "minimum": 0},
"storage_path": {"type": "string"},
"access_level": {"type": "string", "enum": ["public", "internal", "confidential"]}
}
}

该规则会自动校验元数据的字段存在性、类型和取值范围,避免格式混乱
将你的旧档案数据导出为CSV格式,确保包含上述必选字段的对应列,例如旧CSV中“创建时间”列的原始值为2024/05/20,无需修改列名,后续会通过代码映射
将以下代码完整复制到process_archives.py中,若你的旧CSV列名不同,需修改代码中row.get()的参数(如旧CSV的“文档类型”列改为“类型”则无需修改,改为“档案分类”则改为row.get('档案分类')):
import csv
import uuid
import json
from jsonschema import validate, ValidationError
加载元数据校验规则
with open('metadata_schema.json', 'r', encoding='utf-8') as f:
metadata_schema = json.load(f)
定义旧档案CSV路径和输出路径
old_csv_path = 'old_archives.csv'
new_json_path = 'standard_archives.json'
standard_archives = []
读取旧CSV并转换
with open(old_csv_path, 'r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
转换字段到标准格式
standard_row = {
"arch_id": str(uuid.uuid4()),
"arch_type": row.get('类型', 'other').strip().lower(),
"create_time": row.get('创建时间', '').strip().replace('/', '-') + 'T00:00:00Z',
"creator": row.get('创建者', '未知').strip(),
"file_size": int(row.get('文件大小', 0)),
"storage_path": row.get('存储路径', '').strip(),
"access_level": row.get('权限', 'internal').strip().lower()
}
校验格式
try:
validate(instance=standard_row, schema=metadata_schema)
standard_archives.append(standard_row)
except ValidationError as e:
print(f"档案{row.get('文件名')}校验失败:{e.message}")
输出标准化元数据到JSON
with open(new_json_path, 'w', encoding='utf-8') as f:
json.dump(standard_archives, f, indent=2, ensure_ascii=False)
print(f"标准化完成,共处理{len(standard_archives)}条档案")
在终端进入文件所在文件夹,执行命令:python3 process_archives.py,等待运行完成,终端会打印校验失败的档案信息(若有),若无异常则生成standard_archives.json
打开standard_archives.json,随机抽取10条档案,检查以下内容:arch_id是否为36位UUID、create_time是否为ISO 8601格式、权限字段是否在指定范围内
在终端执行以下命令,可快速校验全部元数据是否符合标准:python3 -c 'import json; from jsonschema import validate; schema=json.load(open("metadata_schema.json")); data=json.load(open("standard_archives.json")); [validate(item, schema) for item in data]; print("全部元数据校验通过")',输出“全部元数据校验通过”则可继续落地
将标准化后的元数据文件与档案文件一一对应,或导入你的档案管理系统,执行前必须备份旧档案和旧数据,确保无异常后正式启用标准化元数据