1
0
Fork 0
hugo/docs/content/en/functions/data/GetCSV.md

3.6 KiB

title description categories keywords params expiryDate
data.GetCSV Returns an array of arrays from a local or remote CSV file, or an error if the file does not exist.
functions_and_methods
aliases returnType signatures
getCSV
[][]string
data.GetCSV SEPARATOR INPUT... [OPTIONS]
2026-02-19

{{< deprecated-in 0.123.0 >}} Instead, use transform.Unmarshal with a global resource, page resource, or remote resource.

See the remote data example.

{{< /deprecated-in >}}

Given the following directory structure:

my-project/
└── other-files/
    └── pets.csv

Access the data with either of the following:

{{ $data := getCSV "," "other-files/pets.csv" }}
{{ $data := getCSV "," "other-files/" "pets.csv" }}

[!note] When working with local data, the file path is relative to the working directory.

You must not place CSV files in the project's data directory.

Access remote data with either of the following:

{{ $data := getCSV "," "https://example.org/pets.csv" }}
{{ $data := getCSV "," "https://example.org/" "pets.csv" }}

The resulting data structure is an array of arrays:

[
  ["name","type","breed","age"],
  ["Spot","dog","Collie","3"],
  ["Felix","cat","Malicious","7"]
]

Options

Add headers to the request by providing an options map:

{{ $opts := dict "Authorization" "Bearer abcd" }}
{{ $data := getCSV "," "https://example.org/pets.csv" $opts }}

Add multiple headers using a slice:

{{ $opts := dict "X-List" (slice "a" "b" "c") }}
{{ $data := getCSV "," "https://example.org/pets.csv" $opts }}

Global resource alternative

Consider using the resources.Get function with transform.Unmarshal when accessing a global resource.

my-project/
└── assets/
    └── data/
        └── pets.csv
{{ $data := dict }}
{{ $p := "data/pets.csv" }}
{{ with resources.Get $p }}
  {{ $opts := dict "delimiter" "," }}
  {{ $data = . | transform.Unmarshal $opts }}
{{ else }}
  {{ errorf "Unable to get resource %q" $p }}
{{ end }}

Page resource alternative

Consider using the Resources.Get method with transform.Unmarshal when accessing a page resource.

my-project/
└── content/
    └── posts/
        └── my-pets/
            ├── index.md
            └── pets.csv
{{ $data := dict }}
{{ $p := "pets.csv" }}
{{ with .Resources.Get $p }}
  {{ $opts := dict "delimiter" "," }}
  {{ $data = . | transform.Unmarshal $opts }}
{{ else }}
  {{ errorf "Unable to get resource %q" $p }}
{{ end }}

Remote resource alternative

Consider using the resources.GetRemote function with transform.Unmarshal when accessing a remote resource to improve error handling and cache control.

{{ $data := dict }}
{{ $url := "https://example.org/pets.csv" }}
{{ with try (resources.GetRemote $url) }}
  {{ with .Err }}
    {{ errorf "%s" . }}
  {{ else with .Value }}
    {{ $opts := dict "delimiter" "," }}
    {{ $data = . | transform.Unmarshal $opts }}
  {{ else }}
    {{ errorf "Unable to get remote resource %q" $url }}
  {{ end }}
{{ end }}