简体   繁体   中英

Premailer - UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position

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?

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!

Had the same issue but where the html source required utf_8 encoding.

Prepending the premailer command with an explicit encoding format via PYTHONIOENCODING , resolved the issue for me:

PYTHONIOENCODING=utf_8 python -m premailer -f reports.html --external-style ./style/jbehave-core.css > emailable-reports.html

It might be the other way around in your case:

PYTHONIOENCODING=ascii python -m premailer -f report.html --external-style ./style/somestyle.css > emailable-report.html

More information regarding setting/changing the default encoding in python: Changing default encoding of Python?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM