Fear_Schwier
Fear_Schwier

Reputation: 3

How to add Headers to read output from Python CSV File?

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.

Upvotes: 0

Views: 3111

Answers (4)

Melroy van den Berg
Melroy van den Berg

Reputation: 3175

  1. Open the file
  2. Read/parse the file via csv.reader
  3. Read the first row using next as header row
  4. Read the remaining rows. Check if the column is integer, float or string.
  5. You now have results array with a nice key/value dict for each row with there responding header names.

This is what I use:

with open(args.filename) as csvfile:
    headers = []
    result = []

    reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    # Read headers from first row
    headers = next(reader)
    for row in reader:  # iterate the remaining rows
        parsed_row = {}
        for header, value in zip(headers, row):
            if value.isdigit():  # Check if value is a digit
                parsed_row[header] = int(value)  # Convert to integer if it's a digit
            elif value.replace('.', '', 1).isdigit():  # Check if value is a float
                parsed_row[header] = float(value)  # Convert to float if it's a float
            else:
                parsed_row[header] = value  # Otherwise, keep it as a string
        result.append(parsed_row)

Upvotes: 0

Adrian Klaver
Adrian Klaver

Reputation: 19665

Use DictReader from here csv.

cat food.csv                                                                                                                                                                                                           
Item #,Price,Quantity,Name
1, 5.99$,1,Blueberry Muffin


import csv
with open('food.csv') as csv_file:
    reader = csv.DictReader(csv_file,delimiter=",", quotechar='"')
    for row in reader:
        print(dict(row))

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

Upvotes: 0

AirSquid
AirSquid

Reputation: 11903

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

Upvotes: 1

Harsh Narwariya
Harsh Narwariya

Reputation: 565

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.

Upvotes: 3

Related Questions