Python在2000个字符后拆分字符串
我正在开发一个可以返回维基百科文章摘要的discord机器人。但有一个问题,一些摘要超过2000个字符,这超过了discord的字符限制。有没有办法将字符串拆分为多条消息 我要拆分的字符串是Python在2000个字符后拆分字符串,python,string,split,discord,wikipedia-api,Python,String,Split,Discord,Wikipedia Api,我正在开发一个可以返回维基百科文章摘要的discord机器人。但有一个问题,一些摘要超过2000个字符,这超过了discord的字符限制。有没有办法将字符串拆分为多条消息 我要拆分的字符串是str(wikipedia.search(query))(整个内容都在一个嵌入块中): 这里有一个解决方案: article = "5j5rtOf8jMePXn7a350fOBKVHoAJ4A2sKqUERWxyc32..." # 4000 character string i used
str(wikipedia.search(query))
(整个内容都在一个嵌入块中):
这里有一个解决方案:
article = "5j5rtOf8jMePXn7a350fOBKVHoAJ4A2sKqUERWxyc32..." # 4000 character string i used
chunklength = 2000
chunks = [article[i:i+chunklength ] for i in range(0, len(article), chunklength )]
print(len(chunks))
输出
2
关于如何使用它的扩展:
for chunk in chunks:
embedVar = discord.Embed(title="article name",
description=chunk ,
color=0x9CAFBE)
await ctx.send(embed=embedVar)
要扩展Darina的评论,请在发布到discord之前拼接字符串
posted_string = str(wikipedia.search(query))[:2000]
embedVar = discord.Embed(title=str(query),
description=posted_string,
color=0x9CAFBE) await message.channel.send(embed=embedVar)
“字符串”是一个字符数组。当您使用[:2000]将其分配给另一个变量时,您告诉解释器将数组开头的所有字符都放在第2000个字符之前,但不包括第2000个字符
编辑:
正如Ironkey在评论中提到的,硬编码值是不可行的,因为我们不知道一篇文章有多少个字符。请尝试以下未经测试的代码:
wiki_string = str(wikipedia.search(query))
string_length = len(wiki_string)
if string_len < 2000:
embedVar = discord.Embed(title=str(query),
description=wiki_string,
color=0x9CAFBE)
await message.channel.send(embed=embedVar)
else:
max_index = 2000
index = 0
while index < (string_length - max_index):
posted_string = wiki_string[index:max_index]
embedVar = discord.Embed(title=str(query),
description=posted_string,
color=0x9CAFBE)
await message.channel.send(embed=embedVar)
index = index + max_index
posted_string = wiki_string[index-max_index:]
embedVar = discord.Embed(title=str(query),
description=wiki_string,
color=0x9CAFBE)
await message.channel.send(embed=embedVar)
wiki\u string=str(wikipedia.search(查询))
string\u length=len(wiki\u字符串)
如果字符串长度<2000:
embedVar=discord.Embed(title=str(查询),
description=wiki\u字符串,
颜色=0x9CAFBE)
wait message.channel.send(嵌入=embedVar)
其他:
最大指数=2000
索引=0
而索引<(字符串长度-最大索引):
posted\u string=wiki\u string[索引:max\u index]
embedVar=discord.Embed(title=str(查询),
description=posted\u字符串,
颜色=0x9CAFBE)
wait message.channel.send(嵌入=embedVar)
索引=索引+最大索引
posted\u string=wiki\u string[index-max\u index:]
embedVar=discord.Embed(title=str(查询),
description=wiki\u字符串,
颜色=0x9CAFBE)
wait message.channel.send(嵌入=embedVar)
如果这不起作用,请让我知道它失败的地方。谢谢
string[:2000]
不起作用?有没有办法将我的字符串拆分为多条消息?
再次拼接字符串。在本例中,posted_string2=str(wikipedia.search(query))[2000:4000]。这将获取从索引2000到的字符,但不包括索引4000。如果您不知道长度怎么办;硬编码不是一个足够的解决方案我用应该可以工作的代码编辑了我的答案,不管文章的大小。干得好,这就足够了!
wiki_string = str(wikipedia.search(query))
string_length = len(wiki_string)
if string_len < 2000:
embedVar = discord.Embed(title=str(query),
description=wiki_string,
color=0x9CAFBE)
await message.channel.send(embed=embedVar)
else:
max_index = 2000
index = 0
while index < (string_length - max_index):
posted_string = wiki_string[index:max_index]
embedVar = discord.Embed(title=str(query),
description=posted_string,
color=0x9CAFBE)
await message.channel.send(embed=embedVar)
index = index + max_index
posted_string = wiki_string[index-max_index:]
embedVar = discord.Embed(title=str(query),
description=wiki_string,
color=0x9CAFBE)
await message.channel.send(embed=embedVar)