[英]How to split arguments with a decimal point
我是 python 的新手,需要一些幫助
這是我的問題的一個例子:
@bot.command()
async def example(ctx, *, arg, arg2):
await ctx.message.delete()
r = requests.get(f"https://example.api/image?first_text={arg}sec_text={arg2}")
await ctx.send(r)
如果我執行這樣的命令: {prefix}example argument one text, argument two text
它應該返回example.api/image?first_text=argument one text&sec_text= argument two text
這是您可以拆分論點的一種方法。 您可以使用.split()
來分隔arguments
,如下所示。
@client.command()
async def split(ctx, *, arg):
x = arg.split(',')
for arg in x:
await ctx.send(arg)
工作結果:
編輯: But how do I implement it in the api url
您可以做的一件事是將其作為列表進行檢查。 你注意到我之前的代碼是如何通過x
變量循環的嗎? 當您await ctx.send()
或單獨print
變量x
時,它會為您提供一個列表。
@client.command()
async def split(ctx, *, arg):
x = arg.split(',')
print(x)
上面的代碼將打印: ['arg one', ' arg two', ' another arg']
。 有了這些信息,您可以瀏覽列表。
@client.command()
async def split(ctx, *, arg):
x = arg.split(',')
await ctx.send(f"""
Here's one argument: {x[0]}
And another: {x[1]}
And a third one while we're at it: {x[2]}
""")
您可以使用 asyncio 來執行工作。 您可以使用 **kwargs 傳遞參數和值的字典。 當在異步代碼中檢測到等待時,它會放棄代碼執行並開始執行等待任務。
async def func1(**kwargs):
result=0
parameters={}
for key, value in kwargs.items():
#print(key+":"+str(value))
parameters[key]=value
for i in range(parameters['parm1'],parameters['parm2']):
result+=i
await asyncio.sleep(0.02)
print("func1",result)
return result
async def func2(number, exponent):
result=0
for i in range(100):
num1=pow(number+1,exponent)
result+=num1
print("func2",result)
await asyncio.sleep(0.01)
return result
dictParms={'parm1':300,'parm2':400}
async def main():
await asyncio.gather(
asyncio.create_task(func1(**dictParms)),
asyncio.create_task(func2(2,4)),
)
if __name__ == "__main__":
asyncio.run(main())
試試這個方法
@bot.command()
async def example(ctx, *args):
await ctx.message.delete()
for arg in args:
print(f"{arg}")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.