2016-11-30 8 views
-3

私のコードで何かを修正する際にいくつか問題があります。プログラムを一度実行すると、テキストファイルの1行に複数回項目が印刷されます。私のプログラムは、テキストファイルに何度も項目を印刷しますか?

This is what happens when I run my code. The text file is so the user knows what items need to be restocked

This is my CSV file which is called 'blank file' and is attached to the variable blank. It is where the new stock levels are written

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

すべてはその問題から離れて正常に動作します。

+0

を削除するには追加をご覧ください[なぜ、仲間のユーザーが削除されている感謝、あなたが私の質問からですか?](http://meta.stackoverflow.com/questions/328379/why-私の質問からあなたに感謝しています。 – Aurora0001

答えて

0

これを修正する方法を理解しました。

私はちょうどfor field in row: