Forums

genshi html transformation

UPDATE: NEVERMIND. I THINK I'LL USE JINGA2 INSTEAD. I AM THINKING MAYBE I WAS WRONG TO THINK GENSHI COULD WORK SEPARATE FROM A WEB FRAMEWORK? I'LL LEAVE THIS HERE IN CASE ANYONE ELSE HAVING SIMILAR ISSUES STUMBLES THIS WAY.

I don't think this question is specific to PythonAnywhere but I am lost as to why it won't work and am looking for any help someone may volunteer...

I was trying to implement the recipe from the Genshi page on Transforming HTML documents on PythonAnywhere.

import os, sys
from genshi.input import HTMLParser
from genshi.template import Context, MarkupTemplate

def transform(html_filename, tmpl_filename):
     tmpl_fileobj = open(tmpl_filename)
     tmpl = MarkupTemplate(tmpl_fileobj, tmpl_filename)
     tmpl_fileobj.close()

     html_fileobj = open(html_filename)
     html = HTMLParser(html_fileobj, html_filename)
     print tmpl.generate(Context(input=html)).render('xhtml', doctype='html')
     html_fileobj.close()

if __name__ == '__main__':
    transform(sys.argv[1], sys.argv[2])

When it runs it keeps giving the error UnicodeError: source returned bytes, but no encoding specified. I tracked it down to the '.render' part of the command but my attempts and encoding or decoding failed. Anyone familiar with Genshi know how to fix it?

I saw that a lot of people reported this problem happening with the shift from 0.6 to 0.7. And here says that it was remedied in the 0.7dev. But I cannot tell what I should do to fix this in the example. I even found this, but it also didn't make it clear how to fix It seems it is probably basic but eludes me thus far.