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

**URL:** <https://forum.devtalk.com/t/creating-a-csv-file-in-python-need-guidance-and-code-example/121652>\
**Category:** General Dev Chat\
**Tags:** python\
**Created:** [24 August 2023 08:00 UTC](https://forum.devtalk.com/t/creating-a-csv-file-in-python-need-guidance-and-code-example/121652 "2023-08-24T08:00:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![harwind](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/h/c77e96/32.png) [@harwind](https://forum.devtalk.com/u/harwind)\
**Post date:** [24 August 2023 08:00 UTC](https://forum.devtalk.com/t/creating-a-csv-file-in-python-need-guidance-and-code-example/121652/1 "2023-08-24T08:00:01Z")

</div>

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:

```auto
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 [this](https://www.scaler.com/topics/how-to-create-a-csv-file-in-python/), but I was unable to do so. Could someone provide me an example of accomplishing this using the Python csv module? I would be very grateful for any advice!

---

<div class="post-metadata">

**Author:** ![jkdiaz](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/j/dbc845/32.png) [@jkdiaz](https://forum.devtalk.com/u/jkdiaz)\
**Post date:** [26 August 2023 10:24 UTC](https://forum.devtalk.com/t/creating-a-csv-file-in-python-need-guidance-and-code-example/121652/2 "2023-08-26T10:24:02Z")

</div>

I never did anything complex with regards to csv, so I did use the common csv module too. Have you tried to ask ChatGPT? 🙂
