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