0

I am trying to use Markdown with python, but I am having trouble:

import markdown

layers = [1,2,3]
lr = 2
dir = 'result'

with open('readme.md', 'a+') as f:
    f.write('## Layer sizes')
    for layer in layers:
        f.write('* {}'.format(layer))
    f.write('## Learning rate is {}'.format(lr))
    f.write('## Init')
    f.write('Add note about init')
    markdown.markdownFromFile(input=f, output=dir+'/out.html')

I get the error can't concat str to bytes which is a common question, for example here. I tried different arrangement of changing to bytes or back to string, but I still can't get it to work; instead I just get a slightly different error, but the whole theme is str and byte compatibility. I'd appreciate any help.

1
  • You should add which line exactly causes the problem Dec 17, 2021 at 23:26

1 Answer 1

1

Well it seems like, markdown package works only on Binary files instead of Text files, so encode the individual strings that you're writing to the readme.md file and pass in file pointers as the input and output instead of just the name of the file.

import markdown as md

layers = [1,2,3]
lr = 2
dir = 'result'

with open('readme.md', 'ab+') as f:
    f.write('## Layer sizes\n'.encode())

    for layer in layers:
        f.write(f'* {layer}\n'.encode())

    f.write(f'## Learning rate is {lr}\n'.encode())
    f.write('## Init\n'.encode())
    f.write('Add note about init\n'.encode())

md.markdownFromFile(input=open("readme.md", "rb"), output=open("out.html", "wb"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.