CSV files

+1 Matthew Swart · November 13, 2015
Hi Ladies and gentledudes.

I am at the moment working with CSV library and have worked out most if not all the options and how to use them but I am stuck on a problem. I am wondering if it is possbile to make a template in of say a receipt in excel, then change it to a CVS file format, make a copy of the file and rename it to say receipt1.cvs then receipt2.csv atc atc and append the information in that copy so that I can save it. 

I have a python project I am busy with at the moment and have the receipt working but I can't figure out how to change the receipt number or how to append that receipt so I can add VAT, price without VAT and the total and figured out that making a template would be the best option. Please find a copy of my code so far and any help would be awesome and thanks in advance for it.






























































































































#Import csv library for us later in the program.

import csv




#Declare variables.

price = 0 #Used for calculations

drinks = 'go' #Used to keep the loop running while the customer orders once they type finish program will drop out of loop

n = 0 #Used for the receipt loop to cycle through the arrays and print out all the info




vat = 0




#Arrays used to store the info collected in the drinks loop

item = []

volume = []

amount = []

product = []







def drinkReceipt(num1):

"""

:rtype : object

"""

return {'Product': product[num1],

'Item': item[num1],

'Size': volume[num1],

'Price': amount[num1]}







headings = {'Product',

'Item',

'Size',

'Price'}




while drinks.lower() != 'finish':

print('Welcome')

print('Please select your poison')

drinks = input('Beer, wine, whiskey or finish')

print(drinks)




if drinks.lower() == 'beer':




product.append('Beer')

beers = input('Would you like lager or stout')

size = input('Pint or half pint')




if size.lower() == 'pint':




item.append(beers)

volume.append(size)

amount.append('5')

price += 5




else:




item.append(beers)

volume.append(size)

amount.append('3')

price += 3




elif drinks.lower() == 'wine':




product.append('Wine')

wine = input('Would you prefer red or white')

size = input('Would you like a glass or bottle')




if size.lower() == 'glass':




item.append(wine)

volume.append(size)

amount.append('3')

price += 3




else:




item.append(wine)

volume.append(size)

amount.append('12')

price += 12




elif drinks.lower() == 'whiskey':




product.append('Whiskey')

whiskey = input('Would you like Jameson or Bells')

size = input('Would you like a single or double')




if size.lower() == 'single':




item.append(whiskey)

volume.append(size)

amount.append('4')

price += 4




else:




item.append(whiskey)

volume.append(size)

amount.append('8')

price += 8










receipt = open('r.csv', 'w')

w = csv.DictWriter(receipt, fieldnames=headings, dialect='excel')

w.writeheader()







while n < len(product):

print(product[n], ' ', item[n], ' ', volume[n], ' ', amount[n])

receipt_dict = drinkReceipt(n)

w.writerow(receipt_dict)

n += 1




print()

vat = price * 0.23

exclusive = price - vat

print(' VAT = ', vat)

# reciept.write(' VAT = ', vat)

print('Price excl = ', exclusive)

# reciept.write('Price excl = ', exclusive)

print('Total cost = ', price)

# reciept.write('Total cost = ', price)




#

# total = str(product+item+volume+amount)

# receipt.write(total)

# receipt.close()

 

Post a Reply

Replies

Oldest  Newest  Rating
0 Matthew Swart · November 13, 2015
Ahhh hell sorry but scroll down to see code I aint a clue what I did wrong.
  • 1

Python

106,960 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator