簡體   English   中英

從 Python 中的文本文件讀取

[英]Reading from a text file in Python

我正在嘗試制作一個程序來選擇藝術家並從他們的歌曲中打印一個單詞(所有藝術家和歌曲行都在 a.txt 中,應該隨機選擇)。

到目前為止,我已經得到了這個:

def main():
    import time
    import os

    os.system('cls')
    print("███╗   ███╗██╗   ██╗███████╗██╗ ██████╗     ██████╗ ██╗   ██╗██╗███████╗")
    print("████╗ ████║██║   ██║██╔════╝██║██╔════╝    ██╔═══██╗██║   ██║██║╚══███╔╝")
    print("██╔████╔██║██║   ██║███████╗██║██║         ██║   ██║██║   ██║██║  ███╔╝") 
    print("██║╚██╔╝██║██║   ██║╚════██║██║██║         ██║▄▄ ██║██║   ██║██║ ███╔╝ ") 
    print("██║ ╚═╝ ██║╚██████╔╝███████║██║╚██████╗    ╚██████╔╝╚██████╔╝██║███████╗")
    print("╚═╝     ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝     ╚══▀▀═╝  ╚═════╝ ╚═╝╚══════╝")
    print("")
    print("")
    print("Made By *****")
    time.sleep(2)

content = []
with open('songs.txt', 'r') as f:
    for line in f:
        content.append(line.strip('\n'))

    for i in content:
        if 'song 1' == i:
            print("")
        else:
            pass

接下來我該怎么辦?

據我了解,您有一個 song.txt 文件,需要將其作為由行分隔的列表,然后將結果保存在另一個列表中。 所以,你會這樣做的方式是:

def main():
    import time
    import os

    os.system('cls')
    print("███╗   ███╗██╗   ██╗███████╗██╗ ██████╗     ██████╗ ██╗   ██╗██╗███████╗")
    print("████╗ ████║██║   ██║██╔════╝██║██╔════╝    ██╔═══██╗██║   ██║██║╚══███╔╝")
    print("██╔████╔██║██║   ██║███████╗██║██║         ██║   ██║██║   ██║██║  ███╔╝") 
    print("██║╚██╔╝██║██║   ██║╚════██║██║██║         ██║▄▄ ██║██║   ██║██║ ███╔╝ ") 
    print("██║ ╚═╝ ██║╚██████╔╝███████║██║╚██████╗    ╚██████╔╝╚██████╔╝██║███████╗")
    print("╚═╝     ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝     ╚══▀▀═╝  ╚═════╝ ╚═╝╚══════╝")
    print("")
    print("")
    print("Made By *****")
    time.sleep(2)

content = []
for line in open('songs.txt','r').readlines():
    content.append(line.strip('\n'))

for i in content:
    if 'song 1' == i:
        print("")
    else:
        pass

您可以使用隨機 function。

在代碼的頂部:

import random

然后運行:

index = random.randint(0,len(content))

稍后索引它只需使用:

print(content[index])

所以:

import random
def main():
    import time
    import os

    os.system('cls')
    print("███╗   ███╗██╗   ██╗███████╗██╗ ██████╗     ██████╗ ██╗   ██╗██╗███████╗")
    print("████╗ ████║██║   ██║██╔════╝██║██╔════╝    ██╔═══██╗██║   ██║██║╚══███╔╝")
    print("██╔████╔██║██║   ██║███████╗██║██║         ██║   ██║██║   ██║██║  ███╔╝") 
    print("██║╚██╔╝██║██║   ██║╚════██║██║██║         ██║▄▄ ██║██║   ██║██║ ███╔╝ ") 
    print("██║ ╚═╝ ██║╚██████╔╝███████║██║╚██████╗    ╚██████╔╝╚██████╔╝██║███████╗")
    print("╚═╝     ╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═════╝     ╚══▀▀═╝  ╚═════╝ ╚═╝╚══════╝")
    print("")
    print("")
    print("Made By *****")
    time.sleep(2)

content = []
with open('songs.txt', 'r') as f:
    for line in f:
        content.append(line.strip('\n'))
index = random.randint(0, len(content)-1)
print(content[index])

暫無
暫無

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

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