Jake Price
Jake Price

Reputation: 169

YAML to Python dictionary with correct data types using built-in functionality only

I have a variable that contains 4 lines of YAML key/values that is used as frontmatter in a Markdown file.

I need to put it into a dictionary, but when I do this I want the date: value to become a datetime object, and the tags: value(s) to become a list.

I'm not sure how to do this. I can put the key/values into a dictionary, but I'm not sure how to go about "converting" date: and tags: to the correct types before, or during, the creation of the dictionary.

This is how I am currently doing it without the correct types:

>>> import re
>>> frontmatter = """
... id: 20201222163206
... title: Lorem Ipsum Dolor Sit Amet
... date: 2020-12-22 16:32:06
... tags: [lorem, ipsum, dolor, sit, amet]
... """
>>> frontmatter_dict = dict(re.findall(r"(.*): (.*)", frontmatter))
>>> print(frontmatter_dict)
{'id': '20201222163206', 'title': 'Lorem Ipsum Dolor Sit Amet', 'date': '2020-12-22 16:32:06', 'tags': '[lorem, ipsum, dolor, sit, amet]'}

This is what I want the dictionary to be structured like (with the correct types, as specified above):

{'id': 20201222163206, 'title': 'Lorem Ipsum Dolor Sit Amet', 'date': datetime.datetime(2020, 12, 22, 16, 32, 6), 'tags': ['lorem', 'ipsum', 'dolor', 'sit', 'amet']}

I can accomplish it in the example above using the PyYAML module, but it's crucial that I only use built-in Python modules for this, so using PyYAML is not a solution in this case.

I've had a look through the source code for PyYAML to see if I can figure out how to do it, but alas my Python skills aren't yet good enough to figure it out.

As mentioned, this is how I accomplish it with PyYAML, but I need to do it with built-in modules only.

>>> import yaml
>>> frontmatter = """
... id: 20201222163206
... title: Lorem Ipsum Dolor Sit Amet
... date: 2020-12-22 16:32:06
... tags: [lorem, ipsum, dolor, sit, amet]
... """
>>> frontmatter_dict = yaml.load(frontmatter, Loader=yaml.FullLoader)
>>> print(frontmatter_dict)
{'id': 20201222163206, 'title': 'Lorem Ipsum Dolor Sit Amet', 'date': datetime.datetime(2020, 12, 22, 16, 32, 6), 'tags': ['lorem', 'ipsum', 'dolor', 'sit', 'amet']}

Upvotes: 0

Views: 1162

Answers (1)

MaxYarmolinsky
MaxYarmolinsky

Reputation: 1139

after constructing dictionary

import datetime


frontmatter_dict['date'] = datetime.datetime.strptime(frontmatter_dict['date'], '%Y-%m-%d %H:%M:%S')

for the tag

frontmatter_dict['tag'] = frontmatter_dict['tag'][1:-1].split(',')

Upvotes: 1

Related Questions