> For the complete documentation index, see [llms.txt](https://docs.rewst.help/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rewst.help/~/revisions/IAv4UYPrwRFrpB0SUQW8/documentation/jinja/common-jinja-examples/creating-csvs.md).

# Generate CSVs dynamically with Jinja

Creating Comma-Separated Values (CSV) files is a common requirement in various applications. Jinja templating offers a simple and efficient way to generate CSV files dynamically. This article demonstrates how to create a CSV file from data using Jinja templates and how to set headers dynamically, providing a versatile solution for your data processing needs.

### **Create a CSV File with Jinja**

To create a CSV file using Jinja, follow these steps:

1. **Prepare Your Data:** First, organize your data into a list of dictionaries. Each dictionary represents a row in the CSV, with keys representing column headers and corresponding values.

   ```django
   {%- set csv_data = [
       {
       "name": "rewsty",
       "address": "123 rewsty lane",
       "timezone": "EST"
       }
   ] -%}
   ```
2. **Generate CSV Headers Dynamically:** Use Jinja filters to dynamically set the CSV headers. In the provided example, the `fieldnames` parameter is populated with the keys from the first dictionary in the `csv_data` list.

   ```django
   {{- csv_data | csv(fieldnames = csv_data[0].keys() | list) -}}
   ```

   Here, `csv_data[0].keys() | list` generates a list of keys from the first dictionary in `csv_data`, ensuring dynamic header generation.

### **Example output**

The above Jinja code will generate a CSV output similar to the following:

```django
name,address,timezone
rewsty,123 rewsty lane,EST
```

#### **Benefits of dynamic CSV generation with Jinja**

1. **Flexibility:** Dynamic header generation allows you to adapt your CSV format based on the structure of your data.
2. **Simplicity:** Jinja's concise syntax simplifies the process of transforming data into CSV format.
3. **Automation:** As your data changes, Jinja can adapt the CSV headers automatically, streamlining your workflow.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.rewst.help/~/revisions/IAv4UYPrwRFrpB0SUQW8/documentation/jinja/common-jinja-examples/creating-csvs.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
