我想创建一个遍历包含列表的嵌套字典的函数。对于与关键字匹配的每个值,该函数将其替换为另一个关键字。
函数是否返回另一个字典或是否更改主字典并不重要。
我试图分开案例: - 如果数据是一个字典,做一些 - 如果数据是一个列表,做别的
字典:
data_dict = {
"name": "AAAAAA",
"content": "BBBBBB",
"dat": [
{
"author": {
"name": "CCCCCC",
"surname": "DDDDDD",
},
"title": "FFFFFF",
"color": 15258703,
"fields": [
{
"name": "GGGGGG",
"value": "HHHHHH",
},
{
"name": "IIIIII",
"value": "JJJJJJ",
}
],
"thumbnail": {
"url": "KKKKKK"
},
"image": {
"url": "LLLLLL"
},
"footer": {
"text": "MMMMMMM",
"icon_url": "NNNNNN"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
现在我只是想改变每个值,看看我是否也在迭代。我可以打印所有data_dict更改的值,但我无法在字典中管理它...
def recursive_replace_valuer(data, match, repl):
if isinstance(data, list):
for l in data:
recursive_replace_valuer(l, match, …Run Code Online (Sandbox Code Playgroud) 我需要制作一个机器人来侦听服务器中写入的消息,同时接受命令。
# Create the Discord client
client = discord.Client()
client = commands.Bot(command_prefix = '!')
client.remove_command('help')
@client.event
async def on_ready():
print('ready')
@client.event #ricerca messaggi
async def on_message(message):
# Ignore messages made by the bot
if(message.author == client.user):
return
a = ''
a += message.embeds[0]["description"]
if a == 'abcdef':
print(' aaaaa ')
@client.command()
async def hello():
await client.say('hello')
client.run('token')
Run Code Online (Sandbox Code Playgroud)
我怎样才能让它工作?我认为问题在于机器人在第一个事件中继续循环……我阅读了 sub_process 但我不明白如何使用它。
我正在尝试在Mac OS上安装MySQLdb for Python.当我数字pip install MySQL-pythonshell返回到这个:
Collecting MySQL-python
Using cached https://files.pythonhosted.org/packages/a5/e9/51b544da85a36a68debe7a7091f068d802fc515a3a202652828c73453cad/MySQL-python-1.2.5.zip
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "", line 1, in
File "/private/var/folders/9h/2lp9kx993ygbrfk1lxr0sz500000gq/T/pip-install-7xyyBe/MySQL-python/setup.py", line 17, in
metadata, options = get_config()
File "setup_posix.py", line 53, in get_config
libraries = [ dequote(i[2:]) for i in libs if i.startswith(compiler_flag("l")) ]
File "setup_posix.py", line 8, in dequote
if s[0] in "\"'" and s[0] == s[-1]:
IndexError: string index out of range
----------------------------------------
Command "python setup.py … python ×2
python-3.x ×2
bots ×1
dictionary ×1
discord ×1
discord.py ×1
json ×1
macos ×1
mysql-python ×1
recursion ×1