【问题标题】:I am needing to extract the variables from the end of a large string/dict/something我需要从大字符串/dict/something 的末尾提取变量
【发布时间】:2021-09-01 22:11:13
【问题描述】:

我正在寻找一个可以在这里使用的简单解决方案:这是我的代码。

import discord
import json
from discord.ext import commands
print("Starting bot...")

TOKEN = ''
client = commands.Bot(command_prefix = '.')

@client.event
async def on_message(message):
    embeds = message.embeds
    for embed in embeds:
        emb = str(embed.to_dict())
        if 'owned' in emb:
            owned = object('title')
            print(owned)
print("Bot is ready!")
client.run(TOKEN)

(我确实有一个不和谐的机器人令牌,我将其删除以向大家展示)无论如何,我的目标是能够获取 emb,它来自脚本,如下所示:

{'footer': {'text': 'Item Type: Sellable'}, 'thumbnail': {'width': 75, 'url': 'https://cdn.discordapp.com/emojis/714980893999104051.png', 'proxy_url': 'https://images-ext-1.discordapp.net/external/c4teYBjoGAhxnygEZ5F2GlHWzReCIg_xEOX1PPtZdIQ/https/cdn.discordapp.com/emojis/714980893999104051.png', 'height': 75}, 'color': 15684432, 'type': 'rich', 'description': "This item's purpose is to be collected or sold. Nothing more, nothing less.\n\n**BUY** - Not able to be purchased\n**SELL** - ⏣ 1,680 (multiplier included)\n**TRADE** - 2k - 7k", 'title': '**Common Fish** (697 owned)'}

并且只能取最后一部分,即 697。我的主要问题是有时为 500,有时仅为 2,但我希望能够提取它并将其用作变量。请记住,线条的长度会有所不同。提前致谢!

【问题讨论】:

    标签: python string discord discord.py


    【解决方案1】:

    我没有足够的声誉来发表评论,所以我必须写下这个答案。这很容易,实际上你只需要基本的 python 字符串操作技能就可以了

    假设,您的字典/json 被分配给变量 x

    x = {'footer': {'text': 'Item Type: Sellable'}, 'thumbnail': {'width': 75, 'url': 'https://cdn.discordapp.com/emojis/714980893999104051.png', 'proxy_url': 'https://images-ext-1.discordapp.net/external/c4teYBjoGAhxnygEZ5F2GlHWzReCIg_xEOX1PPtZdIQ/https/cdn.discordapp.com/emojis/714980893999104051.png', 'height': 75}, 'color': 15684432, 'type': 'rich', 'description': "This item's purpose is to be collected or sold. Nothing more, nothing less.\n\n**BUY** - Not able to be purchased\n**SELL** - ⏣ 1,680 (multiplier included)\n**TRADE** - 2k - 7k", 'title': '**Common Fish** (697 owned)'}
    

    我们可以通过x['title']获取标题

    第一步是在第一个左括号(处拆分

    这将使它成为x['title'].split('(')

    这会给我们一个输出列表['**Common Fish**', '697 owned)']

    那么我们将索引 1,因为我们需要此列表中的第二项(因为您需要的数字就在其中)

    这将使它成为x['title'].split('(')[1]

    现在我们进一步拆分它,但这次我们没有为 .split() 提供任何参数,它只会在空格字符上拆分。

    这将使它成为x['title'].split('(')[0].split()

    这样,您将再次获得如下列表: ['your desired number', 'owned)']

    那么剩下的就是将此列表索引为0!

    这将使它成为x['title'].split('(')[1].split()[0]

    这会给我们your number (697 in this case)

    适用于任何x 号码

    
    x = {'footer': {'text': 'Item Type: Sellable'}, 'thumbnail': {'width': 75, 'url': 'https://cdn.discordapp.com/emojis/714980893999104051.png', 'proxy_url': 'https://images-ext-1.discordapp.net/external/c4teYBjoGAhxnygEZ5F2GlHWzReCIg_xEOX1PPtZdIQ/https/cdn.discordapp.com/emojis/714980893999104051.png', 'height': 75}, 'color': 15684432, 'type': 'rich', 'description': "This item's purpose is to be collected or sold. Nothing more, nothing less.\n\n**BUY** - Not able to be purchased\n**SELL** - ⏣ 1,680 (multiplier included)\n**TRADE** - 2k - 7k", 'title': '**Common Fish** (697 owned)'}
    
    my_num = x['title'].split('(')[1].split()[0]
    

    【讨论】:

    • 嘿-现在我收到一条错误消息,我收到以下错误消息:“TypeError:字符串索引必须是整数”,当我查看它时,它说这可能是由于有一些东西不是括号内的整数,当我将它们更改为括号时,我收到此错误“TypeError:'str' object is not callable”,有什么建议吗?
    • 不和谐联系我Pizerite#5648
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2018-03-23
    相关资源
    最近更新 更多