当前位置: 首页 > article >正文

读取 json 文件

读取json文件

with open(file_path) as f:
        json_data = json.load(f)  

json 文件的格式如下:

[
    {
        "img_path": "path/to/image1.jpg",
        "question": "这张图片中有什么?",
        "candidates": [
            "猫",
            "狗",
            "鸟",
            "鱼"
        ],
        "correct_ans": "猫"
    },
    {
        "img_path": "path/to/image2.jpg",
        "question": "这张图片中的主要颜色是什么?",
        "candidates": [
            "红色",
            "蓝色",
            "绿色",
            "黄色"
        ],
        "correct_ans": "蓝色"
    }
]

获取 json 文件的内容:

for i, data in enumerate((json_data)):
    # 获取图像文件路径
    filename = data ["img_path"]
    # 初始化消息字符串,包含问题文本和换行符
    message = data ["question"] + "\n"
    # 遍历选项和候选答案
    for choice, ans in zip(["A.", "B.", "C.", "D."], data ["candidates"]):
        # 将选项和答案添加到消息字符串中
    	message += f"{choice} {ans}\n"
        # 如果存在正确答案且当前答案是正确答案,记录正确选项的字母
    	if "correct_ans" in data and ans == data ["correct_ans"]:
    		correct_choice = choice[0]

    # 非必须,根据语言设置添加相应的提示信息
    # if args.language == "en":
        # # 英文提示
    	# message = message + "Answer with the option's letter from the given choices directly.\n"
    # elif args.language == "ch":
        # # 中文提示
    	# message = message + "请直接回答正确选项的字母\n"
    # else:
        # # 中文提示
    	# aise NotImplementedError("Does not support languages other than English (en) and Chinese (ch) yet. ")

http://www.kler.cn/a/349508.html

相关文章:

  • 工资保证金监管平台有多重要?
  • 2024.10月12日--- SpringMVC异常处理
  • Spring Boot集成Spring Security之自动装配
  • Flink 04 | 窗口介绍 - 无界数据流的核心
  • MediaGo:革新视频下载体验的开源神器
  • 安卓上的iso 是哪几个gain 相乘
  • 软件开发----SQL基础每日刷题(转载于牛客)
  • 前端入门学习之css盒子原则
  • 基于K-means和RFM模型的电商行业用户画像及商品个性化推荐研究
  • 【华为】基于华为交换机的VLAN配置与不同VLAN间通信实现
  • QT--QPushButton设置文本和图标、使能禁能、信号演示
  • MySQL-02.概述-安装配置
  • 力扣hot100--二叉树
  • 大模型从入门到应用——LangChain:模型(Models)-[大型语言模型(LLMs):基础知识!
  • IO进程---day2
  • 集师知识付费小程序:打造培训机构在线教育的金字招牌 集师知识付费系统 集师知识付费小程序 集师知识服务系统 集师线上培训系统 集师线上卖课小程序
  • react中css样式隔离
  • 【Docker系列】Docker查看镜像架构
  • 基于JavaFX+Mysql实现(PC)足球联赛评分系统
  • C++ 右值引用深入理解:特性、优化与底层