[英]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.