我需要在2d列表中有一个For循环,除非我希望循环一直持续到文件末尾,因为长度未知。你知道吗
这是我的密码:
while sales_search == 'Y':
columns = 13
with open('sales.txt', 'rt', encoding = 'utf8') as inf:
data = list(csv.reader(inf,skipinitialspace=True))
data = [i for i in data if i]
rows = 0
for row in range(rows):
for col in range(columns):
if (columns==0):
found = False
print ('****************')
search = input ('What year do you want to search for:')
if year == search:
print ('****************')
print ('Year:', (data[row][0]))
print ('The amount made from the Toyota Klugers is $',
format((data[row][1]),',.2f'))
print ('The amount made from the Nissan Patrols is $',
format((data[row][2]),',.2f'))
print ('The amount made from the Ford Territorys is $',
format((data[row][3]),',.2f'))
print ('The total amount made from all three cars is $',
format((data[row][4]),',.2f'))
print ('The bonus amount is $',
format((data[row][5]),',.2f'))
print ('The bonus contributed through the sale of the Toyota Klugers is $',
format((data[row][6]),',.2f'))
print ('The bonus contributed through the sale of the Nisssan Patrols is $',
format((data[row][7]),',.2f'))
print ('The bonus contributed through the sale of the Ford Territorys is $',
format((data[row][8]),',.2f'))
print ('The additional bonus for the Toyota Kluger is $',
format((data[row][9]),',.2f'))
print ('The additional bonus for the Nissan Patrol is $',
format((data[row][10]),',.2f'))
print ('The additional bonus for the Ford Territory is $',
format((data[row][11]),',.2f'))
print ('The total bonus is $',
format((data[row][12]),',.2f'))
found = True
如果能提供帮助,我将不胜感激:)
现在你正在做
for row in range(rows)
,也就是for row in range(0)
。这实际上是在一个空列表上循环。你知道吗相反,使用
for row in data
,它将把行分配给文件中的实际数据段:我想你想要
你可以在
data
上迭代。它是表示文件行的数组。你知道吗相关问题 更多 >
编程相关推荐