DK1MI.radio

bear2hugo.py - Migrating from Bear Blog to Hugo

I’ve written the following small Python script to migrate a Bear Blog to Hugo:

import csv
import os

with open('post_exports.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    os.makedirs("./pages", exist_ok = True)
    os.makedirs("./posts", exist_ok = True)
    for row in csv_reader:
        dir = ""
        if row[11] == "True":
            dir = "pages/"
        else:
            dir = "posts/"
        with open(dir + row[4]+'.md', 'w') as post:
            publish = row[9]
            if publish == 'False':
                draft = "true"
            else:
                draft = "false"
            post.write("+++\n")
            post.write("slug = '" + row[4] + "'\n")
            post.write("title = '" + row[3] + "'\n")
            post.write("date = " + row[6] + "\n")
            post.write("draft = " + draft + "\n")
            post.write("tags = " + row[8] + "\n")
            post.write("+++\n")
            post.write(row[12])

It’s not really sophisticated but it does the job. Here’s all you need to know:

Thank you for reading! If you have any comments or questions, please send me an e-mail.

#Hugo #Python