Jump to content

Creating a CSV File in Python: Need Guidance and Code Example


Techi11
 Share

Recommended Posts

I'm relatively new to Python and I'm trying to create a CSV file from data within my program. I've read a bit about the csv module, but I'm not entirely sure about the best approach to create a CSV file and populate it with data. Could someone guide me through the process and provide a code example?

Let's say I have a list of dictionaries containing data about products, like so:

products = [
    {"ProductID": 1, "ProductName": "Widget A", "Price": 10.99},
    {"ProductID": 2, "ProductName": "Widget B", "Price": 15.99},
    {"ProductID": 3, "ProductName": "Widget C", "Price": 8.49}
]

I want to create a CSV file named products.csv with the following headers: "ProductID", "ProductName", and "Price", and then populate it with the data from the products list.

 I tried looking for a solution by visiting numerous forums and websites like Scaler, but I was unable to do so. Could someone provide me an example of how to accomplish this using the Python csv module? I would be very grateful for any advice!

Link to comment
Share on other sites

The csv module provides two classes for writing data to a CSV file: csv.writer and csv.DictWriter. The csv.writer class writes data to a CSV file in the form of lists, while the csv.DictWriter class writes data in the form of dictionaries. Since your data is already in the form of a list of dictionaries, it would be more convenient to use the csv.DictWriter class.

Here’s an example of how you can create a CSV file named products.csv and populate it with data from your products list:

import csv

products = [
    {"ProductID": 1, "ProductName": "Widget A", "Price": 10.99},
    {"ProductID": 2, "ProductName": "Widget B", "Price": 15.99},
    {"ProductID": 3, "ProductName": "Widget C", "Price": 8.49}
]

with open('products.csv', 'w', newline='') as csvfile:
    fieldnames = ["ProductID", "ProductName", "Price"]
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

    writer.writeheader()
    for product in products:
        writer.writerow(product)
Copy

In this example, we first import the csv module. Then, we define our list of dictionaries containing data about products. Next, we open a new file named products.csv in write mode ('w') using the open() function. We also pass the newline='' parameter to ensure that newlines are handled correctly regardless of the platform.

We then define the field names (headers) for our CSV file and create a csv.DictWriter object by passing the file object (csvfile) and the field names (fieldnames) as arguments. We use the writeheader() method to write the headers to the first row of our CSV file.

Finally, we iterate over each dictionary in our products list and use the writerow() method to write each dictionary (product) as a row in our CSV file.

I hope this helps! Let me know if you have any further questions or if there’s anything else I can assist you with. 😊

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...