0

A am using Flask and I want to generate a csv file using a Jinja2 template. According to the Jinja2 documentation

A Jinja template is simply a text file. Jinja can generate any text-based format (HTML, XML, CSV, LaTeX, etc.). A Jinja template doesn’t need to have a specific extension: .html, .xml, or any other extension is just fine.

The problem is that Jinja2 is very proficient at inserting whitespace and line-breaks and these kind of ruin the csv output which should just be a file of records each record being a list of comma-separated values.

I need to use Jinja2 rather than the csv module because all the data I need is already stored in a data object designed to feed to Jinja2 (I usually generate HTML but sometimes JSON and now I want to generate csv).

Here is my template, note that dp is a "data page" object and contains the headers and values.

{% set g = namespace(comm0 = '', comma = '') %}

{# Header #}
{% for ix in range(dp.field_count) -%}

    {{ g.comm0 }}
    "{{ dp.fields[ix].label }}"
    {% set g.comm0 = ', ' %}

{%- endfor %}

{# Rows #}
{% for record in dp.records -%}

    {% set g.comma = '' %}

    {% for ix in range(dp.field_count) -%}

        {{ g.comma }}
        "{{ record.row[ix].value }}"
        {% set g.comma = ', ' %}

    {%- endfor %}

{%- endfor %}

And here is what the output looks like (already with some whitespace eliminated by using the - symbol).





    "Staff Type"
    , 
    "Item"
    , 
    "Billable?"
    , 
    "Description"
    , 
    "Hourly Rate"
    , 
    "Date Changed"
    , 
    "Tag"






        "AC"
        , 
        ""
        , 
        "-1"
        , 
        "Associate Consultant"
        , 
        "20"
        , 
        ""
        , 
        ""



        "ACS"
        , 
        ""
        , 
        "-1"
        , 
        "Consultant"
        , 
        "29"
        , 
        ""
        , 
        ""



        "ADMIN"
        , 
        ""
        , 
        "0"
        , 
        "Administration"
        , 
        "20"
        , 
        ""
        , 
        ""

And here is what a csv should look like.

"Staff Type" ,  "Item" ,  "Billable?" ,  "Description" , "Hourly Rate" , "Date Changed", "Tag"
"AC", "", "-1", "Associate Consultant",  "20", "", ""
"ACS", "", "-1", "Consultant", "29", "", ""
"ADMIN", "", "0", "Administration", "20", "", ""

So how do I eliminate the line breaks within a record, and put a line break at the end of each record, and get rid of the remaining unnecessary whitespace.

Mark Kortink
  • 1,770
  • 4
  • 21
  • 36

1 Answers1

1

It turns out I pretty much needed to put those - symbols everywhere. This template works and gives a csv by eliminating whitespace between everything except where I want a line break.

{% set g = namespace(comm0 = '', comma = '') -%}

{# Header #}
{%- for ix in range(dp.field_count) -%}

    {{- g.comm0 -}}
    "{{- dp.fields[ix].label -}}"
    {%- set g.comm0 = ', ' -%}

{%- endfor %}

{# Rows #}
{%- for record in dp.records -%}
    {%- set g.comma = '' -%}
    {%- for ix in range(dp.field_count) -%}

        {{- g.comma -}}
        "{{- record.row[ix].value -}}"
        {%- set g.comma = ', ' -%}

    {%- endfor %}
{% endfor %}

Refer to the manual.

Mark Kortink
  • 1,770
  • 4
  • 21
  • 36