eyeseast / python-frontmatter

Parse and manage posts with YAML (or other) frontmatter
http://python-frontmatter.rtfd.io
MIT License
333 stars 42 forks source link

yaml.representer.RepresenterError: ('cannot represent an object', 'Home | UZPG') #68

Closed Uzay-G closed 4 years ago

Uzay-G commented 4 years ago

I was really happy to find this package because it will be super helpful for a project of mine.

I was using it like this when i got the error yaml.representer.RepresenterError: ('cannot represent an object', 'Home | UZPG') :

            data = {'title': 'Home | UZPG', 'date': String, 'id': id ...}

            # convert to markdown by setting text content
            dataobj = frontmatter.loads(post_content)

           # save front matter
            for k, v in data.items():
                dataobj[k] = v

            print(dataobj.content)
            print(dataobj.metadata)
            print(frontmatter.dumps(dataobj))

Output:

... long post body

{'type': 'bookmark', 'url': 'http://uzpg.me', 'desc': '', 'title': 'Home | UZPG', 'date': '07-07-20', 'tags': [], 'id': 1}

  File "/home/uzay/Documents/Projects/archivy/venv/lib/python3.6/site-packages/yaml/representer.py", line 231, in represent_undefined
    raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', 'Home | UZPG')

I tried it with other title attributes and it always fails. Do you know what the problem could be? Thanks a bunch!

eyeseast commented 4 years ago

No idea. I tried to recreate it, based on what you put here, and got no error: https://gist.github.com/eyeseast/67e11cdbe029f87577dc023b12e60980

Did I miss anything there?

Uzay-G commented 4 years ago

Oh apparently the variable i was using to store the title must have not been in string format or something because directly using str(title) fixed the problem.

Thanks