You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python将JSON格式宝可梦数据转换为可读自然语句?

用Python实现JSON宝可梦数据转易读文本方案

这种转换完全可以用Python实现,效果远优于单纯删除括号——能精准提取关键信息,按照你想要的自然语言格式输出。以下是具体实现步骤和代码:

实现思路

  1. 先把你提供的JSON片段补全为合法JSON对象(包裹在{}中),再读取解析;
  2. 遍历每个宝可梦条目,提取特性、携带道具、Tera属性、技能等核心信息;
  3. 对列表类型字段做格式处理:单个值直接输出,多个道具/属性用“或”连接,技能用逗号分隔;
  4. 生成符合要求的自然语言文本。

示例代码

import json
from itertools import chain

# 假设JSON数据已保存为pokemon_data.json(补全为合法JSON格式)
with open('pokemon_data.json', 'r', encoding='utf-8') as f:
    pokemon_data = json.load(f)

# 遍历处理每个宝可梦
for name, info in pokemon_data.items():
    # 处理特性字段
    abilities = info['abilities']
    ability_str = abilities[0] if len(abilities) == 1 else '或'.join(abilities)
    
    # 处理携带道具字段
    items = info['items']
    if len(items) == 1:
        item_str = items[0]
    else:
        item_str = '、'.join(items[:-1]) + ' 或 ' + items[-1]
    
    # 提取所有角色下的Tera属性并去重
    all_tera_types = list(set(chain.from_iterable(role['teraTypes'] for role in info['roles'].values())))
    tera_str = all_tera_types[0] if len(all_tera_types) == 1 else '、'.join(all_tera_types[:-1]) + ' 或 ' + all_tera_types[-1]
    
    # 提取所有角色下的技能并去重
    all_moves = list(set(chain.from_iterable(role['moves'] for role in info['roles'].values())))
    move_str = '、'.join(all_moves)
    
    # 生成目标格式文本
    result = f"{name},特性为{ability_str},可携带道具{item_str},Tera属性为{tera_str},可学习技能{move_str}。"
    print(result)

示例输出

针对你提供的三条数据,输出如下:

Abomasnow,特性为Snow Warning,可携带道具Assault Vest 或 Light Clay,Tera属性为Ice 或 Water,可学习技能Aurora Veil、Blizzard、Earthquake、Ice Shard、Wood Hammer。
Alomomola,特性为Regenerator,可携带道具Leftovers 或 Rocky Helmet,Tera属性为Steel,可学习技能Body Slam、Liquidation、Mirror Coat、Protect、Wish。
Altaria,特性为Natural Cure,可携带道具Heavy-Duty Boots 或 Leftovers,Tera属性为Steel 或 Flying 或 Ground,可学习技能Brave Bird、Defog、Earthquake、Haze、Roost、Will-O-Wisp、Dragon Dance。

如果需要调整输出格式(比如改成英文表述、修改连接词),只需修改result变量的模板即可。

内容的提问来源于stack exchange,提问作者Lewis Simcoates

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 16:45:20