如何在Python中保存并重新加载Azure AnalyzeResult对象?
问题:如何将JSON加载的字典转回AnalyzeResult类实例
已执行操作:
- 生成文档分析结果:
with open(pdf_path, "rb") as f: poller = document_intelligence_client_sections.begin_analyze_document( "prebuilt-layout", f.read(), content_type="application/pdf", output_content_format=ContentFormat.MARKDOWN, ) result = poller.result() type(section_layout) # 输出:azure.ai.documentintelligence.models._models.AnalyzeResult
- 将结果保存为JSON:
with open("data/section_layouts/result.json", "w") as f: json.dump(section_layout.as_dict(), f)
- 加载JSON文件:
with open("result.json", "r") as f: data = json.load(f)
补充信息:
使用Azure Document Intelligence SDK中的DocumentIntelligenceClient:
from azure.ai.documentintelligence import DocumentIntelligenceClient document_intelligence_client_sections = DocumentIntelligenceClient( endpoint=service_endpoint, credential=default_credential) with open(pdf_path, "rb") as f: poller = document_intelligence_client_sections.begin_analyze_document( "prebuilt-layout", f.read(), content_type="application/pdf", output_content_format=ContentFormat.MARKDOWN, ) result = poller.result()
- 分析结果对象
section_layout的类型为azure.ai.documentintelligence.models._models.AnalyzeResult - 该对象没有
.to_dict()和.from_dict()方法
需求:
将加载JSON得到的字典格式数据,转换回AnalyzeResult类的实例格式。
内容的提问来源于stack exchange,提问作者Droid-Bird
相关产品推荐
相关产品推荐

