question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UnicodeEncodeError: 'ascii' codec can't encode character

See original GitHub issue

Hi Peter, I’ve created an html document with bootstrap styling and when I try and run Premailer from the CLI I get 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 have tried everything that I can think of to try and avoid this, but can’t seem to make any progress. What would usually cause this error and do you know a way to fix it? Thanks!

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dAnjoucommented, Jun 27, 2016

I was running into a similar issue:

❯ git diff master staging | sh ~/Downloads/ansi2html.sh | python -m premailer > /tmp/diff.html          
Traceback (most recent call last):
  File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/max/.local/lib/python2.7/site-packages/premailer/__main__.py", line 148, in <module>
    sys.exit(main(sys.argv[1:]))
  File "/home/max/.local/lib/python2.7/site-packages/premailer/__main__.py", line 143, in main
    options.outfile.write(p.transform(pretty_print=options.pretty))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 126700: ordinal not in range(128)

https://github.com/pixelb/scripts/blob/4d075026866dff074749f938c111c7a0478309c1/scripts/ansi2html.sh

Found this article http://chase-seibert.github.io/blog/2014/01/12/python-unicode-console-output.html and the following command worked:

git diff master staging | sh ~/Downloads/ansi2html.sh | PYTHONIOENCODING=UTF-8 python -m premailer > /tmp/diff.html
0reactions
nrlakincommented, Jun 20, 2017

Python 2.7.10, premailer 3.0.1. It looks like you need to use the -o option to get the bug; if I omit the -o, it prints the output to stdout, with the degree sign rendered as unicode. Here’s a simple script to reproduce:

echo "<html><body><p>&deg;</p></body></html>" > test.html
python -m premailer -f test.html -o out.html

When I run that, I get the exception.

Read more comments on GitHub >

github_iconTop Results From Across the Web

UnicodeEncodeError: 'ascii' codec can't encode character ...
The issue is that when you call str(), python uses the default character encoding to try and encode the bytes you gave it,...
Read more >
Fix Python UnicodeEncodeError: 'ascii' codec can't encode ...
The UnicodeEncodeError we reproduced earlier, is caused due to the fact that when str() method is called, the interpreter will use the default...
Read more >
'ascii' codec can't encode character in position
The Python "UnicodeEncodeError: 'ascii' codec can't encode character in position" occurs when we use the ascii codec to encode a string that ...
Read more >
UnicodeEncodeError: 'ascii' codec can't encode character'
Fix – UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0′: · Set the Python encoding to UTF-8. · Set the environment variables correctly in...
Read more >
'ascii' codec can't encode character '\xe9' in position 117: ...
Description. UnicodeEncodeError: 'ascii' codec can't encode character '\xe9'. Expected behavior. No response. pip version.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found