我在修复代码时遇到了一些问题。当我运行程序一次时,程序会在我的文本文件中的一行上多次打印一个项目。
This is my original CSV file which holds all the product information
这是我的代码:
import csv
data=open('Product information.csv', 'rt')
purchase=csv.reader(data)
blank=open('Blank file.csv', 'wt', newline='')
blank_write=csv.writer(blank)
reorder=open('Reorders.txt', 'wt')
order=input('Please enter the GTIN-8 code of the product you would like to purchase: ')
quantity=int(input('Please enter the amount of this product you want to buy: '))
for row in purchase:
GTIN=row[0]
item=row[1]
price=row[2]
stock=row[3]
reorder_level=row[4]
target_stock=row[5]
if order==GTIN:
stock=int(stock)-quantity
blank_write.writerows([[GTIN,item,price,stock,reorder_level,target_stock]])
data.close()
blank.close()
blank2=open('Blank file.csv', 'rt')
blank_write2=csv.reader(blank2)
choice=input('Please enter yes to check the current stock levels: ')
if choice=='yes':
for row in blank_write2:
for field in row:
GTIN=row[0]
item=row[1]
price=row[2]
stock=int(row[3])
reorder_level=int(row[4])
target_stock=int(row[5])
if stock<=reorder_level:
amount_to_restock=target_stock-stock
reorder_file=('{} {} {} {} {} {} {}'.format(row[0]+' ', row[1]+' ', '{:10.2f}'.format(float(row[2]))+' ', str(stock)+' ', str(reorder_level)+' ', str(target_stock)+' ', str(amount_to_restock)))
reorder.write(reorder_file)
blank2.close()
reorder.close()
除了那个问题外,一切都很好。