2016-03-23 2 views
0

У меня есть файл CSV с моим кодом, который включен в качестве изображения. Проблема в том, что часть, которая записывает в CSV-файл. Это либо не позволяет мне писать в файл, либо говорит, что CSV не читается.CSV не читает

enter image description here

import csv 

def checkstock(): 
    print('''The products that can be stocked are: 
Apples : 46527851 
Chickens : 39647511 
Oranges : 46912848 
Toys  : 63891379 
Pens  : 46873262 
Water  : 38509162''') 

    barcode=input('Please enter the GTIN of the product you want to check the stock for: ') 
    with open('Stock.csv', 'r') as data: 
     file=csv.reader(data) 
     for row in file: 
      if barcode in row: 
       product1=row 
       print(product1) 

    name=product1[0] 
    box=int(product1[1]) 
    currentstock=int(product1[2]) 
    minstock=int(product1[3]) 
    orderbox=int(product1[4]) 
    barcode=int(product1[5]) 

    needstock=int(currentstock-minstock) 
    list3=[] 
    if needstock <= 0: 
     print('Order needed for pens, please order ' + orderbox + 'boxes of' + name) 
     with open('Stock.csv', 'w') as data: 
      file=csv.reader(data) 
      list3.extend(file) 
      overwrite={line[product1,'Yes']} 
      with open('Stock.csv', 'w') as data: 
       writer = csv.writer(data) 
       for line, row in enumerate(list3): 
        data1 = overwrite.get(line, row) 
        writer.writerow(data1) 

    else: 
     print('The current level of ' + name + ' is sufficient.') 
     with open('Stock.csv', 'w') as data: 
      file=csv.reader(data) 
      list3.extend(file) 
      overwrite={line[product1,'No']} 
      with open('Stock.csv', 'wb') as data: 
       writer = csv.writer(data) 
       for line, row in enumerate(list3): 
        data1 = overwrite.get(line, row) 
        writer.writerow(data1) 

    repeat=input('Would you like to check the stock of another item? (Y or N) ') 
    if repeat == 'Y': 
     checkstock() 
    if repeat == 'N': 
     start() 



    choice=input('''Would you like to either:  (Please enter a capial letter) 
    (A) Check stock, 
    (B) Record a delivery of products or, 
    (C) See what needs to be delivered or, 
    (D) Update a stock level: ''') 

    if choice == 'A': 
     checkstock() 
    if choice == 'B': 
     recStock() 
    if choice == 'C': 
     inOrder() 

ответ

0

Я не понимаю код, но я вижу некоторые проблемы:

with open('Stock.csv', 'w') as data: 
     file=csv.reader(data) 
     list3.extend(file) 
     overwrite={line[product1,'Yes']} 

Вы не можете сделать csv.reader с «W» доступ к файл. У вас может быть только csv.writer() Кроме того, я бы рекомендовал не писать в файле, который вы читаете с помощью csv.

Что вы пытаетесь сделать с частью чтения/записи?

Смежные вопросы