From: Andreas K. <an...@ka...> - 2018-12-03 18:31:00
|
OK. I figured it out. Sorry for my beginner questions :-) The solution is, of course, to do the encoding explicitly for the strings I want to write using str.encode(encoding='utf-8') /Andreas mån 3 dec. 2018 kl 19:19 skrev Andreas Kågedal <an...@ka...>: > OK. I figured out that it worked if I opened the file in binary mode > ('wb'). But that made it more problematic to do what I intended, that is > add some html before the generated stuff. Something along the below. This > does not work, since it complaints that "TypeError: a bytes-like object is > required, not 'str'": > > fd_html = open(htmlPath, 'w', encoding='utf-8') > fd_html.write('\n'.join([ > '<!DOCTYPE html>', > '<html>', > '<head>', > ' <meta charset="utf-8">', > ' <title>My Title</title>\n', > ' <link rel="stylesheet" href="%s">\n' % "../my.css", > '</head>\n' > ])) > > markdown.markdownFromFile(input=mdPath, > output=fd_html, > encoding='utf-8', > > extensions=['markdown.extensions.tables', 'markdown.extensions.def_list', > > 'markdown.extensions.footnotes', 'markdown.extensions.fenced_code', > > 'markdown.extensions.codehilite', 'markdown.extensions.attr_list'] > ) > fd_html.close() > > I admit that I am a beginner with python and python-markdown, so I realize > I might be doing beginner mistakes. Do you have a proposal for how I should > do the above? > > /Andreas > > mån 3 dec. 2018 kl 17:46 skrev Andreas Kågedal <an...@ka...>: > >> Hi, >> I am probably doing something fundamentally wrong. I get a 'TypeError: >> write() argument must be str, not bytes' message when I use >> the markdown.markdownFromFile method when with an output file which I have >> already opened for writing: >> >> fd_html = open(htmlPath, 'w', encoding='utf-8') >> >> markdown.markdownFromFile(input=mdPath, >> output=fd_html, >> encoding='utf-8', >> >> extensions=['markdown.extensions.tables', 'markdown.extensions.def_list', >> >> 'markdown.extensions.footnotes', 'markdown.extensions.fenced_code', >> >> 'markdown.extensions.codehilite', 'markdown.extensions.attr_list'] >> ) >> fd_html.close() >> >> If I let markdown.markdownFromFile open it instead it works fine: >> >> markdown.markdownFromFile(input=mdPath, >> output=htmlPath , >> encoding='utf-8', >> >> extensions=['markdown.extensions.tables', 'markdown.extensions.def_list', >> >> 'markdown.extensions.footnotes', 'markdown.extensions.fenced_code', >> >> 'markdown.extensions.codehilite', 'markdown.extensions.attr_list'] >> ) >> >> The reason I want to open it myself if that i need to insert some more >> html before the stuff generated by markdown.markdownFromFile. >> >> What am I doing wrong? >> >> /Andreas >> > |