if productid==row[0]: #This means it runs every row in the reader file which is the csv file in this case
price = row[2]
stocklevel = row[3]
reorderlevel = row[4]
targetlevel = row[5]
total = float(amount)*float(price)
totalprice = float(totalprice)+float(total)
stocklevel = float(stocklevel)-float(amount)
newstock = (stocklevel)
b = open('products.csv', 'w')
a = csv.writer(b)
data = [['row[3]', 'row[4]', 'row[5]'],
['293', '219'],
['54', '13']]
a.writerows(data)
b.close()
基本上我一直收到此错误文件
" E:\ Python Program \ pythonv5.py",第71行,in productlist()文件" E:\ Python Program \ pythonv5.py",第9行,在产品列表中 print(row [0] +"" + row [1] +"" + row [2])IndexError:列表索引超出范围
答案 0 :(得分:0)
尝试:
if len(row) > 0 and productid==row[0]:
#rest of the code here ...
您需要确保行列表具有要编制索引的长度。