简体   繁体   English

只打印一行csv文件

[英]Only printing one line of csv file

I'm trying to print every line in a csv file however it will only print the first one. 我正在尝试打印csv文件中的每一行,但是它只会打印第一行。 Heres the code that is responsible: 这是负责的代码:

    if option == '1':
    with open("songs.csv") as f:
        reader = csv.reader(f)
        for row in reader:
            output = (row[0], row[1], row[2]) #Only outputs 1 song ?
            print (output)

Here is the full code if needed: 如果需要,这是完整的代码:

def list():
option = input ('Enter 1 to print a list of all songs or Enter 2 to print songs in a certin genre: ')
if option == '1':
    with open("songs.csv") as f:
        reader = csv.reader(f)
        for row in reader:
            output = (row[0], row[1], row[2]) #Only outputs 1 song ?
            print (output)

            outputtxt_check = False

            while outputtxt_check == False:
                outputtxt = input ("Would you like to output this list to a text file? Enter 1 for yes or 2 for no: ")

                if outputtxt == '1':
                    text_file = open('list.txt', 'w')
                    text_file.write(str(output))
                    text_file.close()
                    outputtxt_check = True
                    print ("Text File Created Successfully!")
                    menu()

                elif outputtxt == '2':
                    (list)
                    outputtxt_check = True
                    menu()

                else:
                    print('Enter 1 or 2')
                    outputtxt_check = False
                    list()

Thank You in advance :) 先感谢您 :)

The part of the code where you list the songs is working for me. 列出歌曲的代码部分对我有用。

if option == '1':
    with open("songs.csv") as f:
        reader = csv.reader(f)
        for row in reader:
            output = (row[0], row[1], row[2]) #Only outputs 1 song ?
            print (output)

However, the stuff you write after, while it is looping, eg 但是,您在循环时编写的内容(例如)

outputtxt_check = False

                while outputtxt_check == False:
                    outputtxt = input(
                        "Would you like to output this list to a text file? Enter 1 for yes or 2 for no: ")

is happening while in the same for loop. 在同一for循环中发生。 That's why it's printing only one line. 这就是为什么它只打印一行。 You're asking user the question while the for loop is not finished printing all the songs yet. 您正在问用户问题,因为for循环尚未完成所有歌曲的打印。 Try putting the while loop outside/after for loop is done. 尝试将while循环放在for循环的外部/之后。

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

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