Convert csv data to yaml using Powershell

While migrating some photo gallery data from my old website to Hugo, I wanted to convert some exported csv data to yaml for using in Hugo content.

I found this post helpful - Hugo: Insert data into content with a shortcode. But my use case was slightly different. I was creating a new layout while trying to reuse an existing partial template in a Hugo theme.

Quick Script #

I installed powershell-yaml module from PSGallery.

Install-Module -Name powershell-yaml

Import-Module powershell-yaml

Import csv data. My data didn't have a header and had some fields I didn't care about.

$header = 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'photoPath', 'photoDate', 'photoTitle', 'photoDescription', 'C13'
$data = Import-Csv -Path .\hawaii.csv -Header $header

Create a Hash Table and append only the relevant data from csv data.

$pictures = @()
foreach ($row in $data) {$pictures += @{ 'title' = $row.photoTitle; 'description' = $row.photoDescription; 'image' = $row.photoPath; 'thumb' = $row.photoPath;}}
$yml = @{ 'title' = "Hawaii"; 'style' = "style1 medium lightbox onscroll-fade-in"; 'content' = "<em>Dec 2021. Oahu, HI, USA.</em>"; 'pictures' = $pictures}

Convert to Yaml and output to a file

ConvertTo-Yaml $yml | Out-File hawaii.yml

References #

Some references I used for my setup

🙏🙏🙏

Since you've made it this far, sharing this article on your favorite social media network would be highly appreciated 💖! For feedback, please ping me on Twitter.

Published