Home > OS >  How to add Headers to read output from Python CSV File?
How to add Headers to read output from Python CSV File?

Time:09-06

I have a csv file, with simply looks like this:

CSV File

I have the following code, which reads the csv file, and then can then print/ access information in the CSV file.

import csv

class CsvReader:
    
    with open("Items.csv") as fp:
        reader = csv.reader(fp, delimiter=",", quotechar='"')
        next(reader, None)  # skip the headers
        data_read = [row for row in reader]

    print(data_read[0])

I get this as the output from the print

['1', '5.99$, '1', 'Blueberry Muffin']

How would I format this into a dictionary, with the headers as the keys and the information as the elements?

For example, the code would output:

{Item #: 1, Price: 5.99, Quantity: 1, Name: Blueberry Muffin}

I referenced and saw a lot of similarities in this post: How do I read and write CSV files with Python?

but couldnt find any more specifics on how to format the output specifically this way, without using something such as pandas, which I am not looking to use.

CodePudding user response:

If you want dict keys as fields i.e columns then why are you skipping them. Here the easy solution.

import csv

class CsvReader:
    with open("Item.csv") as fp:
        reader = csv.reader(fp, delimiter=",", quotechar='"')
        fields = next(reader)
        data_read = []
        for row in reader:
            data_read.append(dict(zip(fields, row)))

    print(data_read[0])

First store the column names and map them with each row element.

CodePudding user response:

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
  • Related