Range over getCSV array not iterating

I am attempting to import a csv file of contact information :card_index: similar to the example below:


It is broken up by carriage returns "\r" and then taking the resulting array and using split to seperate by commas "," in order to output the resulting values with index for a large data set of contact information. Each line is a contact, each value between commas are contact information (i.e. name, phone number, email)

  {{range $i, $r := getCSV "\r" "/rolodex.csv"}}
  {{$c := split (index $r $i) ","}}
  <dt>{{index $c 0}}</dt>
  <dd>{{index $c 1}}<dd>
  <dd>{{index $c 2}}<dd>

when I run hugo the output html shows the first row of $r and the resulting data is correctly split, however the range does not iterate through $i the value of $i remains 0. The output looks like below for the above data example:


Please help me realize what I am missing.

I haven’t got time to answer specifically your scenario, but I had an initial play with this a couple of days ago to produce a table of data from a csv. Here is a snippet of code I have stored away for later adaptation. It may or may not help:

<div class="responsive-embed">
<table border="1">

    {{ range $i, $r := getCSV .Site.Params.sep .Site.Params.url }}
    {{ if eq $i 0 }}
    {{ range $r }}
    <th>{{ . }}</th>
    {{ if ge $i 1 }}
        {{ range  $r }}
        <td>{{ . }}</td>
    {{ end }}

Edit: Yes, try the above, with the comma being the separator. Ignore the returns, as my csv was laid out like that and the above code worked.

1 Like

Thanks for the quick reply @Jonathan_Griffin , your code suggestion gave me the idea to do some testing and see what the {{ . }} dot contained at different points. I found that it was iterating correctly, I just wasn’t accessing the data correctly.

I first tried replacing the (index $r $i) function with the . alone as {{$c := split . ","}} however it was throwing the following error:

error calling split: unable to cast []string
of type []string to string

So in order to access what was in . from split I needed to access the string within []string so i used (index . 0) to get it in the right format for split to separate by commas. The code I got working is below:

{{range $i, $r := getCSV "\r" "/rolodex.csv"}}
{{$c := split (index . 0) ","}}
<dt>{{index $c 0}}</dt>
<dd>{{index $c 1}}<dd>
<dd>{{index $c 2}}<dd>

This code breaks up the CSV file into rows by each carriage return, "\r" and columns from each comma "," allowing you to iterate through all the rows and place specific column data in different parts of the range contents using index. This makes it easy to use data from a csv that is grouped by line with different types of values in different columns, and also be able to have multiple tables in a single csv file (each line a table with the first column {{index $c 0}} as the table header). I hope this helps someone else looking for an answer to this problem.

further explanation here: [Solved] CSV data key lookup