簡體   English   中英

我試圖讓機器人從 700 行的實際 txt 文件中發送 10 行文本

[英]I'm trying to make a bot send 10 lines of text from an actual txt file of 700 lines

我正在使用 Discord.py 編寫一個不和諧的機器人,但我不知道如何使其工作。

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        for line in jeff_file:
            await ctx.send(line)

該文件包含 700 個單詞,但我希望它發送 5 個或 10 個單詞。有人可以幫助我嗎?

您可以使用words = line.split()將一行分成單詞,計算單詞數,然后使用text = ' '.join(words)將單詞重新連接成單個字符串。

將行拆分為單詞以發送前 n 個單詞

以下代碼將發送文件的前n單詞:

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        words = []
        while len(words) < n:
            line = next(jeff_file)      # will raise exception StopIteration if fewer than n words in file
            words.append(line.split())
        await ctx.send(' '.join(words[:n]))

將行拆分為單詞以發送 n 個隨機單詞

以下代碼將從文件中讀取所有單詞,然后隨機選擇 n 個單詞並發送它們:

import random

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        words = [w for line in jeff_file for w in line.split()]
        n_random_words = random.sample(words, n)   # raises exception ValueError if fewer than n words in file
        # n_random_words = random.choices(words, k=n)  # doesn't raise an exception, but can choose the same word more than once if you're unlucky
        await ctx.send(' '.join(n_random_words))

發送前 n 行

以下代碼將從文件中讀取並發送前 n 行:

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        for _ in range(n):
            line = next(jeff_file)  # will raise exception StopIteration if fewer than n lines in file
            await ctx.send(line)

發送前 n 行,如果文件中少於 n 行,則發送更少的行

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        try:
            for _ in range(n):
                line = next(jeff_file)
                await ctx.send(line)
        except StopIteration:
            pass

發送 n 條隨機線

以下代碼將讀取整個文件並隨機發送 n 行:

import random

n = 5

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
        all_lines = [line for line in jeff_file]
        n_lines = random.sample(all_lines, n)  # will raise exception ValueError if fewer than n words in file
        # n_lines = random.choices(all_lines, k = n)  # doesn't raise an exception, but might choose the same line more than once
        await ctx.send(line)

您可以像這樣使用enumerate並打破循環:

@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    with open('Available.txt', 'r') as jeff_file:
         jeff_lines = jeff_file.readlines()
         for i, line in enumerate(jeff_lines):
                if i == 5:
                    break
                else:
                    await ctx.send(line)

其他答案是有效的,但如果您希望代碼更簡單且只有一行,您可以使用:

n = 5
@client.command(alisases = ['readfile'])
@commands.check(is_it_me)
async def jeff(ctx):
    await ctx.send(' '.join(open('Available.txt', 'r').read().split(' ')[:n]))

這將發送文檔中的前n單詞。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM