0

I recently started Python, so sometimes easiest things might seem challenging and difficult for me.

But anyway; I'm trying to write a basic program which can assist me at current job.

The idea is that I just need to fill in the details related to an order I place and that the program creates an object of it and automatically adds it to a CSV file.

Also with the possiblity to have an overview in shipped orders, forecasted sales, orders pending to be shipped et cetera. It just gives me multiple errors all the time, writerow is not possible due to too many arguments. Then I got only the strings: 'date', 'amount', 'customer' etc as output (in the same column)

For now, I just want to create an object, and get that object written CSV file with the object attributes on the same row in different columns.

Here's my code:

import csv
from datetime import datetime

class Order():

    def __init__(self, amount, customer, status, eta): 
        self.date = datetime.now()
        self.amount = amount
        self.customer = customer
        self.status = status
        self.eta = datetime.strptime(eta, '%m-%d') #Converting string input into datestamp

    def __repr__(self):
        pass

    def add_to_orders(self): #Method to add an order to a CSV file

        csv_file = open('orders.csv', 'w')
        fieldnames = ['Order Number', 'Date', 'Amount', 'Customer', 'Status', 'ETA']
        writer = csv.DictWriter(csv_file, fieldnames=fieldnames, delimiter=',')
        writer.writeheader()

        writer.writerow(self, self.date, self.amount, self.customer, self.status, self.eta)

    def reading():
        with open('orders.csv') as csvfile:
            csvreader = csv.DictReader(csvfile)
            for line in csvreader:
                return line


    SCEO36261634 = Order(1500, 'Jasper', 'Shipped', '12-05')
    print(SCEO36261634.amount)
    SCEO36261634.add_to_orders()

Thanks in advance

Prateek Dewan
  • 1,587
  • 3
  • 16
  • 29
  • 1
    Since you want to add orders to your file, I would recommend opening the file for appending. There is a good article about that [here](https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file-in-python) – Rusty Widebottom May 11 '20 at 17:37
  • 1
    SO seems to best for asking questions. It's hard to see a question in your question. Maybe you can re-phrase it. – rpoleski May 11 '20 at 17:43

1 Answers1

1

You want to append to the file (mode 'a'), and even then you only want to write the CSV header when necessary:

class Order():
    def __init__(self, amount, customer, status, eta): 
        self.date = datetime.now()
        self.amount = amount
        self.customer = customer
        self.status = status
        self.eta = datetime.strptime(eta, '%m-%d')

    def add_to_orders(self):
        fieldnames = ['Order Number', 'Date', 'Amount', 'Customer', 'Status', 'ETA']

        with open(filename, 'a', newline='', encoding='utf8') as csv_file:
            writer = csv.DictWriter('orders.csv', fieldnames=fieldnames, delimiter=',')

            # only write headers if we are appending to a brand-new file
            if file.tell() == 0:
                writer.writeheader()

            writer.writerow([self.date, self.amount, self.customer, self.status, self.eta])

    def read(self):
        with open('orders.csv', 'r', newline='', encoding='utf8') as csvfile:
            yield from csv.DictReader(csvfile)

Also, you're supposed to give a list (of field values) to .writerow().

Other than that's it's smart to always specify the file encoding. utf8 will be the best choice for pretty much all circumstances.

Tomalak
  • 332,285
  • 67
  • 532
  • 628