2016-12-04 10 views
0

私は、アイテム名と在庫が格納されているCSVファイルからデータを収集するプログラムを作成します。私は、例えばリンゴのどれくらいが在庫されているかをユーザに表示したいと思います。入力に基づいてPythonでCSVファイルを編集する

Item #  Item name Item stock Item price 
12345670 Apple  20   0.70 

What would you like to buy?: 12345670 
How much would you like to buy?: 10 

そして、私はさまざまな方法を試してみましたが、それらはすべて私に奇妙なエラーを与える20個のリンゴとユーザーの購入10があるので、私がやろうとしていることは10の新しいフィギュアを表示するには、CSVファイルを編集しています。 私の予想される出力は次のようになります。

Item #  Item name Item stock 
12345670 Apple  20 

What would you like to buy?: 12345670 
How much would you like to buy?: 10 
You have bought 10 apple(s) for a price of £7 
There are 10 apples left in stock 

ユーザが購入、あまりにも多くのリンゴの場合:

Item #  Item name Item stock 
12345670 Apple  20 

What would you like to buy?: 12345670 
How much would you like to buy?: 30 
There are not enough apple(s) in stock to buy that amount 
Would you like to buy all there is?: yes 
You have bought 20 apple(s) for the price of £14 

これは私のコード

import csv 
restart = 10 
list1 = [] 
price = 0 
print("Type 'end' to end") 
while restart == 10: 
     file1 = open("CSV File TASK 2.csv", "rt") 
     file2 = csv.reader(file1) 
     print(" ") 
     order = input("Type the item number of your chosen number: ") 
     if order == 'end': 
       break 
     for row in file2: 
       for field in row: 
         if order in field: 
           amount = int(input("How much of that item?: ")) 
           row3 = int(row[3]) 
           if amount > row3: 
             print("There is not enough of that item in stock") 
             print("Please try again") 
             continue 
           row2 = float(row[2]) 
           row1 = str(row[1]) 
           price = amount * row2 + price 
           newstock = row3 - amount 
           print("You have bought {} {}(s) for the price of £{:.2f}".format(amount, row1, price)) 
           print("Your subtotal is currently at {:.2f}".format(price)) 
           receipt = ("{} {} {} {} {}".format(row[0]+" ", row[1], str(amount), "{:10.2f}".format(float(row[2])), "{:10.2f}".format(amount * float(row[2])))) 
           list1.append(receipt) 
print('\n'.join('{}: {}'.format(*k) for k in enumerate(list1))) 

のPython 3.5.2です。事前に感謝してください。あなたは、この使用することができ

+1

を、あなたは私たちに正確な予想される出力を表示することができますか? – ettanany

+0

@ettanany OKay – FlagShipKILLER

+0

@FlagShipKILLERあなたが試したことを私たちに教えてください。 – eyllanesc

答えて

0

import csv 

f  = open('test.csv', 'r') # Here your csv file 
r  = csv.reader(f) 
lines = [l for l in r] 

ItemX = 0 



Item = input("What would you like to buy? ") 
Amount = input("How much would you like to buy? ") 

for x in range(len(lines)): 
    if lines[x][0] == Item: 
     ItemX = x 
    else: 
     continue1 = True # Does nothing 

f.close() 

fw = open('test.csv', 'w') # Here your csv file 

lines[ItemX][2] = float(lines[ItemX][2]) - float(Amount) 

writer = csv.writer(fw) 
writer.writerows(lines) 

fw.close() 
+0

このコードに問題があります。あなた自身でテストしましたか? – FlagShipKILLER

+0

@FlagShipKILLER、ええ - それは私のためにうまくいった –

+0

あなたはどのPythonのバージョンを実行していますか? – FlagShipKILLER

関連する問題