簡體   English   中英

只打印一行csv文件

[英]Only printing one line of csv file

我正在嘗試打印csv文件中的每一行,但是它只會打印第一行。 這是負責的代碼:

    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)

如果需要,這是完整的代碼:

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()

先感謝您 :)

列出歌曲的代碼部分對我有用。

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: ")

在同一for循環中發生。 這就是為什么它只打印一行。 您正在問用戶問題,因為for循環尚未完成所有歌曲的打印。 嘗試將while循環放在for循環的外部/之后。

暫無
暫無

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

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