There's a couple ways to do this... And I agree that using Pandas is likely overkill for reading simple files. You could argue that even using csv_reader
is overkill. :)
Anyhow, here are 3 variations. All you need to do is capture the labels and use them as the keys in the dictionary. Realize that the methods below will get you a "list of dictionaries" (or "records" type format in pandas-speak). An alternative would be a "dictionary of dictionaries" using the item number as the first key, but in essence that is the same as a list index...so about the same. You could also probably forgo capturing the item number as that is just the index in the resultant list of dicts, but that is nuance.
You might also be interested in capturing them in a named tuple
which is shown in the last variant. Very easy to work with...
# Grocery Reader
import csv
from collections import namedtuple
with open("data.csv") as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
labels = next(reader, None) # capture the headers
result = []
for row in reader: # iterate the remaining rows
pairs = zip(labels, row)
result.append(dict(pairs))
print(result)
# the above isn't real satisfying as the numeric objects are captured as strings.
# so...
with open("data.csv") as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
labels = next(reader, None) # capture the headers
result = []
for row in reader: # iterate the remaining rows
row[0] = int(row[0])
row[1] = float(row[1])
row[2] = int(row[2])
pairs = zip(labels, row)
result.append(dict(pairs))
print(result)
with open("data.csv") as fp:
reader = csv.reader(fp, delimiter=",", quotechar='"')
labels = next(reader, None) # capture the headers
# make lowercase...just for standardization
labels = [t.lower() for t in labels]
Grocery = namedtuple('Grocery', labels)
result = []
for row in reader: # iterate the remaining rows
row[0] = int(row[0])
row[1] = float(row[1])
row[2] = int(row[2])
grocery = Grocery(*row)
result.append(grocery)
for grocery in result:
# the below presumes you know the names inside the named tuple...
print(f'a {grocery.name} costs {grocery.price}')
Yields (data.csv can be inferred)
[{'Item': '1', 'Price': '4.99', 'Qty': '2', 'Name': 'Muffin'}, {'Item': '2', 'Price': '1.25', 'Qty': '6', 'Name': 'Gum'}, {'Item': '3', 'Price': '2.50', 'Qty': '8', 'Name': 'Cookie'}]
[{'Item': 1, 'Price': 4.99, 'Qty': 2, 'Name': 'Muffin'}, {'Item': 2, 'Price': 1.25, 'Qty': 6, 'Name': 'Gum'}, {'Item': 3, 'Price': 2.5, 'Qty': 8, 'Name': 'Cookie'}]
a Muffin costs 4.99
a Gum costs 1.25
a Cookie costs 2.5