这是我遇到问题的代码。这是一个基本的GTIN-8检查器,增加了一个数量输入,然后从CSV文件中的预设值中扣除,但写入CSV是一个新概念,所以我很难让它运作。我需要它从CSV中的值中扣除数量输入,然后如果数量太高则更新CSV中的值。我会很感激一些见解。
主要问题是擦除CSV并没有附加到它,我需要它来更改文件中的值并更新命令
import csv
new_rows = []
count = 1
while count==1:
code=(input("Enter a GTIN-8 code or type done to exit"))
length=len(code)
if code == "done":
f.close()
exit()
if length==8:
int1=int(code[0])
int2=int(code[1])
int3=int(code[2])
int4=int(code[3])
int5=int(code[4])
int6=int(code[5])
int7=int(code[6])
int8=int(code[7])
codeDigit=int(int1*3+int2+int3*3+int4+int5*3+int6+int7*3)
roundup=round(codeDigit, -1)
Bcode8th=int(roundup-codeDigit)
if Bcode8th==int8:
quantity=(int(input("Enter a quantity")))
if quantity <51:
try:
val = int(quantity)
except ValueError:
print("That's not an integer")
changes = {quantity}
with open('Stock.csv', 'rb') as f:
reader = csv.reader(f)
for row in reader:
row_one = row
for key, value in changes.items():
row_one = [ x.replace(key, value)for x in row_one
new_rows.append(row_one)
with open('Stock.csv', 'ab') as f:
writer = csv.writer(f)
writer.writerows(new_rows)
else:
print("Try again")
else:
print("Try again")