0

I am trying to implement a method that adds a new row into an existing csv file that I am using for as my "database"

I have the following code so far (a class and some methods left out for simplification):

My Csv File looks like this:

CSV File

class CsvReader:
    def __init__(self): 
        self.result = []

    def make_dict_items(self):
        with open("Items2.csv") as fp:
            reader = csv.reader(fp)
            labels = next(reader, None)
            result = []
            for row in reader:
                row[0] = int(row[0])
                row[1] = float(row[1])
                row[2] = int(row[2])
                pairs = zip(labels, row)
                self.result.append(dict(pairs))

     def show_available(self):
        for item in self.result:
           print(item)

     def add_item(self):

How would I implement in add_item adding an item to the csv file, that is then added to the list of dicts, and shown when I return all the items in show_available()?

I think I have a start, I tried something along the lines of:

def add_item(self):
       item_num = input("What is the items #?\n")
       price = input("What is the items price?\n")
       quant = input("What is the items quantity?\n")
       name = input("What is the items name?\n")
       with open("Items2.csv") as  fp:
           writer = csv.writer(fp)
           labels = next(reader, None)
       

but wasnt really sure how to make the inputs from the user into each specific row to insert? For example, the item # input should go into a new row, etc. How would this be done?

py_coder1019
  • 95
  • 1
  • 8
  • Any reason you're not using `csv.DictReader()`? – Barmar Sep 08 '22 at 20:35
  • @Barmar no specific reason, I just like the formatting of what I have and am using, even if it may be inefficient. Would it be hard to implement adding items with not using that in my ```make_dict_items()```? – py_coder1019 Sep 08 '22 at 20:40

1 Answers1

1

Turn the input into a dictionary, and add it to the self.result list. Then append the row data to the file.

def add_item(self):
   item_num = int(input("What is the items #?\n"))
   price = float(input("What is the items price?\n"))
   quant = int(input("What is the items quantity?\n"))
   name = input("What is the items name?\n")
   new_row = [item_num, price, quant, name]
   with open("Items2.csv", "a+") as  fp:
       reader = csv.writer(fp)
       fp.seek(0)
       labels = next(reader, None)
       writer = csv.writer(fp)
       new_record = dict(zip(labels, new_row))
       self.result.append(new_record)
       writer.writerow(new_record.values())
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I got `PermissionError: [Errno 13] Permission denied: 'Items2' `. I take it this has to do with the rules on my csv file? Also, with the inputs being put into the new new_record, if I were to show the inventory with my existing method show_available, would the new item be there? – py_coder1019 Sep 08 '22 at 20:52
  • You need write permission on the file to update it. – Barmar Sep 08 '22 at 20:52
  • seems closing the csv file and running the program again fixed it. I now get `TypeError: '_csv.writer' object is not an iterator` – py_coder1019 Sep 08 '22 at 20:56