0

Here is the start of my file:

print("Welcome to Ebs Corp.")
print('Please browse my shopping list\n')
with open('./Catalouge2.csv', 'r') as ebsfile:
    products = []
    for line in ebsfile:
        products.append(line.strip().split(','))
    for item in products:
    print('{} {} £{}'.format(item[0],item[1],item[2]))

My csv file is:

12345678,Blue-Eyes White Dragon,5.60
87654321,Dark Magician,3.20
24681012,Toon Blue-Eyes White Dragon,2.00
10357911,Toon Dark Magician,3.00
54626786,Duel Mat,4.30
85395634,Deck Box,2.50
78563412,Pot of Greed,10.50

I want it to be able to add a header for each items. For the numbers at the start I want it to have 'GTIN' then 'Description' and then 'Price' and I would like them to be in line. Thanks

I want it to look like

GTIN---------------Description-----------------Price
12345678-----------Blue-Eyes White Dragon------5.60
87654321-----------Dark Magician---------------3.20

Here is an example but without all the loops http://pastebin.com/7GepdJSu

Cerbrus
  • 70,800
  • 18
  • 132
  • 147

1 Answers1

1

You should use the csv module. The formatting you need can be achieved using the built-in string formatting. I am assuming you have a header row in your csv as follows

GTIN,description,price

>>> import csv
>>> print_format = '| {0: <10} | {1: <30} | {2: >5} |'
>>> with open('/home/ashish/Desktop/sample.csv') as csvfile:
...     reader = csv.reader(csvfile)
...     print(print_format.format('GTIN', 'Description', 'Price'))
...     for row in reader:
...         print(print_format.format(row[0], row[1], row[2]))
... 
| GTIN       | Description                    | Price |
| GTIN       | description                    | price |
| 12345678   | Blue-Eyes White Dragon         |  5.60 |
| 87654321   | Dark Magician                  |  3.20 |
| 24681012   | Toon Blue-Eyes White Dragon    |  2.00 |
| 10357911   | Toon Dark Magician             |  3.00 |
| 54626786   | Duel Mat                       |  4.30 |
| 85395634   | Deck Box                       |  2.50 |
| 78563412   | Pot of Greed                   | 10.50 |
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90