根据用户参数从字典中获取信息

时间:2020-08-29 08:30:25

标签: python discord.py

我有一个看起来像json的文件:

{
    "result": true,
    "scale": 2048,
    "lang": "en",
    "gameVersion": "14.00",
    "list": [
        {
            "id": "Athena.Location.POI.CattyCorner",
            "name": "CATTY CORNER",
            "x": 1611,
            "y": 1568,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/90d1b0b7f439a0f4afa04ec341ba66bb.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.CoralCastle",
            "name": "CORAL CASTLE",
            "x": 414,
            "y": 383,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/40fabf72c844d90356a44858dd58a0d5.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.CraggyCliffs",
            "name": "CRAGGY CLIFFS",
            "x": 1238,
            "y": 274,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/9d97b0051394e63ad257461de39e4fc5.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.DirtyDocks",
            "name": "DIRTY DOCKS",
            "x": 1879,
            "y": 900,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/1c3d4432d17060925e43bc79d9221339.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.FrenzyFarm",
            "name": "FRENZY FARM",
            "x": 1339,
            "y": 795,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/89a5e03b74e440a50f184a675c1431dd.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.HollyHedges",
            "name": "HOLLY HEDGES",
            "x": 418,
            "y": 1117,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/0de5392181742cb3c154b94c39df8195.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.LazyLake",
            "name": "LAZY LAKE",
            "x": 1436,
            "y": 1393,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/10560da606b371da986666aa5864d7ba.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.MistyMeadows",
            "name": "MISTY MEADOWS",
            "x": 1226,
            "y": 1700,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/ea3c76d59eae41e8c1412afe08c90b37.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.PleasantPark",
            "name": "DOOM'S DOMAIN",
            "x": 904,
            "y": 527,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/d3148db60adbdf179663a575e2fc0a59.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.RetailRow",
            "name": "RETAIL ROW",
            "x": 1801,
            "y": 1315,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/b51570866f81b71cb81f21df11a5443d.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.SaltySprings",
            "name": "SALTY SPRINGS",
            "x": 841,
            "y": 927,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/34fa246ccc15e83da87be772a8a7b292.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.SlurpySwamp",
            "name": "SLURPY SWAMP",
            "x": 678,
            "y": 1486,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/9ec6b7e06728454b7ecafe177013716a.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.SteamyStacks",
            "name": "STEAMY STACKS",
            "x": 1751,
            "y": 395,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/31ff51684cda42cbe86f95126258a6f2.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.SweatySands",
            "name": "SWEATY SANDS",
            "x": 457,
            "y": 733,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/c8beb3770713acdd5b8e7c3c8ba4b226.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.TheAuthority",
            "name": "THE AUTHORITY",
            "x": 1063,
            "y": 981,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/d3aee74dcf86b6cc9ab97c290d7c6cff.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.TheFortilla",
            "name": "THE FORTILLA",
            "x": 418,
            "y": 1700,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/c65a53f48ca3efad15693419f151e10c.png"
                }
            ]
        },
        {
            "id": "Athena.Location.POI.WeepingWoods",
            "name": "WEEPING WOODS",
            "x": 752,
            "y": 1234,
            "images": [
                {
                    "type": "overview",
                    "url": "https://media.fortniteapi.io/images/poi/acc23133a1e8daadc1b2d11cbc6c971f.png"
                }
            ]
        }
    ]
}

我正在尝试制作如下命令:!location Catty Corner,该命令应返回xy的值。我的代码看起来像这样

@commands.command()
async def location(self, ctx, *, poi: str):

抱歉,我还没有写任何东西,因为我想先弄清楚该怎么做。预先感谢。

1 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助。请注意,您必须更改文件名和位置。

我将所需名称命名为upper,因为在json中,该名称都是上位

@commands.command()
async def location(self, ctx, *, pos: str):
    with open('./loc.json') as f:
        data = json.load(f)

    for row in data['list']:
        if row['name'] == pos.upper():
            await ctx.send(f"Your x:{row['x']} and your y:{row['y']}")
            break