简体   繁体   English

如何读取.txt文件中的一行文本

[英]How to Read a Line of Text in a .txt File

I am wanting to know how to make it to where instead of typing everything out for my random choosing message how to use a .txt file instead. 我想知道如何将其放到哪里,而不是为我的随机选择消息键入所有内容,而不是如何使用.txt文件。 Here is my code: 这是我的代码:

   import discord
   from discord.ext.commands import Bot
   from discord.ext import commands
   import asyncio
   import time
   import random
   import ctx
   import timeit
   import traceback
   import logging

   bot_prefix = "!"
   bot = commands.Bot(command_prefix=bot_prefix)


   @bot.event
   async def on_ready():
   print("Bot Online!")
   print("Name: {}".format(bot.user.name))
   print("ID: {}".format(bot.user.id))

@bot.command(pass_context = True)
@commands.cooldown(1, 30, commands.BucketType.user)
async def getalt(ctx):
    msg = ["danielmarmeloreis@gmail.com:slime1234", "danielkrock9@yahoo.com:sagaraghi123", "danielemilerevah@gmail.com:truffles101", "danieldiazlauraagudo@hotmail.com:manolo39", "e1verity@yahoo.com:Blessed1", "daniellachner64@gmail.com:london2004", "dyllonng@gmail.com:t0403752e", "danielgrolik@web.de:26101999", "danielspaniel6011@gmail.com:coolboy12", "dyllindsay@live.com:Spiderman23", "eac141099@hotmail.com:Megustaelpan", "eafut13playgood@hotmail.de:sinanovic1", "dyllynnbell@gmail.com:mdb74523", "danielvtotino@rogers.com:dvt2002", "danilklin17@gmail.com:Vfvjxrf1979", "danieltavazzi@alice.it:tavazzi96", "danilofleischer@web.de:FcBayern69", "danielgibert@hotmail.com:costurera01", "danielkozak51@gmail.com:Jetsfan7", "eagle3_2003@yahoo.com:brophy", "eab202303986@eabdf.br:GDF2005", "danijano1999@hotmail.cl:dact1999", "eaglenebulangc6611@gmail.com:Robotics", "danielsimovski23@gmail.com:kikoman23", "dyllanweich@outlook.com:djdyllan1", "danielarendt282@gmail.com:Lego1234", "danielbrodie04@gmail.com:nirvanna2", "daniele.vavala@gmail.com:milo123456", "e.duckworth11@gmail.com:AgentP00", "danielfelipe142009@hotmail.com:samuel1254", "danielkautz139@googlemail.com:Jagd123", "e_wetzl@yahoo.de:yolo1234", "danielograbek@gmail.com:danielo2000", "e3zofficiel@gmail.com:nintendo64", "e.dundon95@gmail.com:Eoinqzell95", "e.boy612@gmail.com:angels612", "e99wright@gmail.com:Monkey99", "dynodriver52@gmail.com:school09", "e319707@gmail.com:ilovetacos11"]
    await bot.send_message(ctx.message.author, random.choice(msg))
    await bot.send_message(ctx.message.channel, "Alt Has Been Seen To Your DMs")

Where I have the email and passwords I would like to use a .txt file for it instead of me having to type in every one. 我希望在其中使用电子邮件和密码的地方使用一个.txt文件,而不必每次都输入一个。 And as you can see there is only 1 email and pass combo per command usage, so in my txt file I would have every combo on a new line. 如您所见,每个命令的用法只有1个电子邮件和密码组合,因此在我的txt文件中,我会将每个组合都放在一个新行上。

file read and write 文件读写

I'm having a little trouble understanding your problem but if you're just wanting to read in the lines from your email file then store them in a list: 我在理解您的问题时遇到了一些麻烦,但是如果您只是想阅读电子邮件文件中的行,然后将它们存储在列表中:

    emailFile = open("emailFile.txt", "r")
    emails = []
    for email in emailFile:
        emails.append(email)

This expects that your emailFile.txt has a singular email per line 这期望您的emailFile.txt每行包含一个单独的电子邮件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM