简体   繁体   English

从 Python 中的文本文件读取

[英]Reading from a text file in Python

I am trying to make a program that selects a artist and prints a word from their song (all artists and song lines are in a.txt and should be chosen at random).我正在尝试制作一个程序来选择艺术家并从他们的歌曲中打印一个单词(所有艺术家和歌曲行都在 a.txt 中,应该随机选择)。

I have gotten this so far:到目前为止,我已经得到了这个:

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

What should I do next?接下来我该怎么办?

From what I understand, you have a songs.txt file and need to get it as a list seperated by lines then, save the result in another list.据我了解,您有一个 song.txt 文件,需要将其作为由行分隔的列表,然后将结果保存在另一个列表中。 So, the way you would do that is:所以,你会这样做的方式是:

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

You can use a random function.您可以使用随机 function。

At the top of the code:在代码的顶部:

import random

Then run:然后运行:

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

to index it later just use:稍后索引它只需使用:

print(content[index])

So:所以:

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