How to read last row of table with getCSV

Hello all,

I am working with getCSV.
I know how to read the 4th row of a table: { if eq $index 3 }}.
How can I read the last row of the table, that is if I don`t know the index number?

Thanks for your time and best regards

{{ $csv := getCSV "," "/foo.csv" }}
{{ range $k, $v := $csv }}
  {{ if eq $k (sub (len $csv) 1) }}
    This is the last row
  {{ else }}
    ...
  {{ end }}
{{ end }}
2 Likes

Thanks again @jmooring,

your code works and I have read and understood it.

Please allow me to ask a follow up question.

In my shortcode I do the following.

{{ if (and (ne $index 0) (ne $index (sub (len $csv) 1) )) }}
  {{ if index $row 0 }}{{ index $row 0 | markdownify | safeHTML }}{ end }}
  {{ if index $row 1 }}{{ index $row 1 | markdownify | safeHTML }}{ end }}
  {{ if index $row 2 }}{{ index $row 2 | markdownify | safeHTML }}{ end }}
  ...
{{ end }}

Can I use something like
{ if index $row <counter-position> }}{{ index $row <counter-position> | markdownify | safeHTML }}{{ end }} so that I only have to write one line of code for all columns?

Thank you for your time and best regards

I don’t understand the question.

Please share your entire shortcode, the markdown that calls the shortcode, and the CSV file.

Hello @jmooring,

Thanks for your inquiry.

Currently I check for each column 0 to 20 if it exists.
If it does, I insert it.

I don’t want to have to check each column individually for existence, but would like to work with an automatic counter.
Just as the code processes rows as long as there are new ones, I want the code to create new columns as long as there are new columns.

Thank you for your time and best regards

You had asked about my files…

My CSV file ./content/dataset-collections/testfile-csv.csv

"Name","Geboren","Geschätztes Vermögen"
"Friedrich Merz","1955","11 Millionen €"
"Christian Lindner","1979","1,5 Millionen €"
"Claudia Roth","1955","3 Millionen €"
"Jens Spahn","1980","3,5 Millionen €"
"Gerhard Schröder","1944","20 Millionen €"
"Geschätzes Vermögen 2021, Quelle: [Vermoegen.org](https://vermoegen.org/)",,

My (shortened) shortcodethemes/ra-master/layouts/shortcodes/emded-csv.html:

{{ $csv := getCSV "," "content/_datensatzsammlungen/testdatei-csv.csv" }}
<figure>

<table class="sortable">
  {{ range $index, $row := $csv }}

    {{ if eq $index 0 }}
      <thead>
        <tr>
          {{ if index $row 0 }}<th>{{ index $row 0 | markdownify | safeHTML }}</th>{{ end }}
          {{ if index $row 1 }}<th>{{ index $row 1 | markdownify | safeHTML }}</th>{{ end }}
          {{ if index $row 2 }}<th>{{ index $row 2 | markdownify | safeHTML }}</th>{{ end }}
          {{ if index $row 3 }}<th>{{ index $row 3 | markdownify | safeHTML }}</th>{{ end }}
          ...
          {{ if index $row 20 }}<th>{{ index $row 20 | markdownify | safeHTML }}</th>{{ end }}
        </tr>
      </thead>
      <tbody>
    {{ end }}

    {{ if (and (ne $index 0) (ne $index (sub (len $csv) 1) )) }}
        <tr>
          {{ if index $row 0 }}<td>{{ index $row 0 | markdownify | safeHTML }}</td>{{ end }}
          {{ if index $row 1 }}<td>{{ index $row 1 | markdownify | safeHTML }}</td>{{ end }}
          {{ if index $row 2 }}<td>{{ index $row 2 | markdownify | safeHTML }}</td>{{ end }}
          {{ if index $row 3 }}<td>{{ index $row 3 | markdownify | safeHTML }}</td>{{ end }}
          ...
          {{ if index $row 20 }}<td>{{ index $row 20 | markdownify | safeHTML }}</td>{{ end }}
        </tr>
    {{ end }}

  {{ end }}
      </tbody>
</table>

<figcaption>
{{ $csv := getCSV "," "content/_datensatzsammlungen/testdatei-csv.csv" }}
  {{ range $index, $row := $csv }}
    {{ if eq $index (sub (len $csv) 1) }}
    {{ index $row 0 | markdownify | safeHTML }}
    {{ end }}
  {{ end }}
</figcaption>

</figure>

My (shortened) content file content/shortcodes/embed-csv.md:

---
title: CSV
linktitle: CSV

menu: ["sub"]

description: Einbindung einer CSV-Datei in die Webseite

---

# CSV-Datei einbinden

{{< embed-csv file="./content/_datensatzsammlungen/testdatei-csv.csv" >}}

Output

You currently range through the rows.
Within each row, range through the columns.

Example
{{ $csv := getCSV "," "content/_datensatzsammlungen/testdatei-csv.csv" }}
<figure>
  <table class="sortable">
    <thead>
      <tr>
        {{ range (index $csv 0) }}
          <th>{{ . | markdownify }}</th>
        {{ end }}
      </tr>
    </thead>
    <tbody>
      {{ range seq 1 (sub (len $csv) 2) }}
        <tr>
          {{ range index $csv . }}
            <td>{{ . | markdownify }}</td>
          {{ end }}
        </tr>
      {{ end }}
    </tbody>
  </table>
  <figcaption>
    {{ index (index $csv (sub (len $csv) 1)) 0 | markdownify }}
  </figcaption>
</figure>

Hello @jmooring,

thank you very much for your help, your code works.

The more I read it, the more I think I understand it.

Do I understand it correctly?
The dot (.) stands for the particular column at row 0 in the CSV file at this point?

{{ range (index $csv 0) }}
  <th>{ . | markdownify }}</th>

Thanks again and best regards

Yes, you do.

Thank you for answering @jmooring

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.