如何根据特定字符的位置来读取txt文件

问题描述:

我有一段代码读取文件,并从该文件中随机选择一行并通过DM将其作为不一致的bot发送。但是,我希望它读取该部分开始和结束的字符的某个部分。 例如:, 嗨 ,如何根据特定字符的位置来读取txt文件

这是我使用的读取随机线,并通过DM发送它的代码:

emailFile = open("C:/Users/jacob/Downloads/Spotify_premiums.txt", "r") 
emails = [] 
for email in emailFile: 
    emails.append(email) 

@bot.command(pass_context = True) 
@commands.cooldown(1, 30, commands.BucketType.user) 
@commands.has_any_role("| Premium |") 
async def spotifypremium(ctx): 
    msg = emails 
    await bot.send_message(ctx.message.author, random.choice(msg)) 
    await bot.send_message(ctx.message.channel, "Alt Has Been Seen To Your DMs") 
    await bot.purge_from(ctx.message.channel, limit=2) 
    await bot.send_message(ctx.message.author, "Please Wait 30 Seconds Before Using This Command Again. If you do not wait the full time then you won't be sent an alt.") 

这里是我的修订:

emailFile = open("C:/Users/jacob/Downloads/Uplay_Formatted.txt", "r", 
    encoding="utf16").read() 
    parse = False 
    email = [] 
    for com in emailFile.split('\n'): 
     if com.startswith(',Credentials'): 
      parse = True 
     elif com.startswith(',Credentials'): 
      parse = False 
     if parse: 
      email.append(com) 


    @bot.command(pass_context = True) 
    @commands.cooldown(1, 30, commands.BucketType.user) 
    @commands.has_any_role("| Premium |") 
    async def spotifypremium(ctx): 
      msg = email 
      await bot.send_message(ctx.message.author, random.choice(msg)) 
      await bot.send_message(ctx.message.channel, "Alt Has Been Seen 
    To Your DMs") 
      await bot.purge_from(ctx.message.channel, limit=2) 
      await bot.send_message(ctx.message.author, "Please Wait 30 
    Seconds Before Using This Command Again. If you do not wait the full 
    time then you won't be sent an alt.") 

您还可以使用startswith()和的endsWith()例如here

例如,你的情况;它将沿着这些线:

emailFile = open("C:/Users/jacob/Downloads/Spotify_premiums.txt", "r") 
read_emails = emailFile.read() 
emails = [com for com in read_emails.split() if com.startswith(',') and com.endswith(',')] 

根据您的要求,以显示开始和结束之间的内容。试试这个:

emailFile = open("C:/Users/jacob/Downloads/Spotify_premiums.txt", "r", encoding="utf8").read() 
parse = False 
for com in emailFile.split('\n'): 
    if com.startswith(',Credentials'): 
     parse = True 
    elif com.startswith(',Credentials'): 
     parse = False 
    if parse: 
     #do stuff 

如果你只是想从文本文件中获取电子邮件地址。

import re 

emailFile = open("C:/Users/jacob/Downloads/Spotify_premiums.txt", "r", encoding="utf8").read() 
email = [] 
for com in emailFile.split(): 
    if re.match(r'[\w\.-][email protected][\w\.-]+', com): 
     email.append(com) 
+0

评论不适用于扩展讨论;这个对话已经[转移到聊天](http://chat.*.com/rooms/156401/discussion-on-answer-by-felasniper-how-to-read-a-txt-file-based-on-其中-A-SPECI)。 – Andy

尝试使用正则表达式https://docs.python.org/2/library/re.html

你可以匹配以单词'Hi'开头的行re.match('^Hi', line)

^表示行的开始,以及其他一些可能有帮助的特殊字符。

+0

我有一个txt文档中的帐户列表,并有一个逗号在末尾和我想要使用的开始,所以我该如何去做这件事。 –

+0

格式化程度如何?我建议阅读我发给你的链接。 正则表达式就像'\,[^ \,] * \,' – Samantha

+0

有没有一个地方可以给你发送文件? –