Premailer - UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position
Question: I am trying to use `premailer` to transform an html document that I've created
into something that I can email with inline css styling. However when I try
make the transform I am getting the following error:

    
    
    Traceback (most recent call last):
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
     File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
    File "/Users/oldo/Python/virtual-environments/AMS-Journal/lib/python2.7/site-packages/premailer/__main__.py", line 142, in <module>
    sys.exit(main(sys.argv[1:]))
    File "/Users/oldo/Python/virtual-environments/AMS-Journal/lib/python2.7/site-packages/premailer/__main__.py", line 137, in main
    options.outfile.write(p.transform(pretty_print=options.pretty))
    UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 106688: ordinal not in range(128)
    

I've seen that there a plethora of questions by people coming up with similar
problems however I can't seem to make any progress with rectifying the fault.

My file is encoded in `utf-8` and so I am confused as to why I'm getting this
error.

Would anyone have any suggestions on something that I can try to make some
moves forward?


Answer: Well after a lot of faffing around I was finally able to fix the source of the
problem. Obviously I need to learn a bit more about the differences between
text encodings and how to handle them in Python.

The input file that I was handing premailer was encoded in `utf-8` and the
program seems to require `ascii`. I am creating the input file using jinja2
linked with a mongodb and I forced the file to be written to `ascii` and to
ignore errors.

    
    
    import io
    
    # stuff setting up jinja ready to render the template...
    
    renderedTemplate = template.render(context)
    
    email = io.open('email.html', 'w', encoding='ascii', errors="ignore")
    email.write(renderedTemplate)
    email.close()
    

I realise that this is not an ideal solution because the `utf-8` characters
that are not recognised by `ascii` will just be dropped, so there may be a
couple of funny looking words, but hopefully this will not be too much of a
problem. I'm just happy to be moving forwards again with my project!

