2017-10-09 3 views
0

Iファイルを読み取り、そのファイルからランダムに1行を選択し、DMを介してdiscordボットとして送信します。しかし、txtファイルの特定のセクションを読むには、セクションの開始と終了の文字が必要です。 例:、 こんにちは 、特定の文字がある場所に基づいて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.") 

答えて

1

あなたはまた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.stackoverflow.com/rooms/156401/discussion-on-answer-by-felasniper-how-to-read-a-txt-file-based-on- where-a-speci)があります。 – Andy

0

てみてください正規表現を使用してhttps://docs.python.org/2/library/re.html

単語 'Hi'で始まる行を一致させることができますre.match('^Hi', line)

^は行の先頭を示し、その他いくつかの特殊文字が役立つ可能性があります。

+0

私はtxtドキュメントにアカウントのリストを持っています。私は使用したいものの最後と最後にコンマがありますので、どうすればこれをやりますか? –

+0

どのようにフォーマットされていますか?私はあなたに送ったリンクを読むことをお勧めします。 正規表現は '\、[^ \、] * \、' – Samantha

+0

のようなものですが、ファイルを送信できる場所はありますか? –

関連する問題