Naftuli Kay
Naftuli Kay

Reputation: 91780

Unicode error only when running from another program?

I have a fun program that displays temperatures. Everything works fine, until I try to use it from Conky:

Traceback (most recent call last):
  File "/home/naftuli/Documents/i7zdaemon/get-i7z-value", line 85, in <module>
    main()
  File "/home/naftuli/Documents/i7zdaemon/get-i7z-value", line 28, in main
    get_temp_value(args)
  File "/home/naftuli/Documents/i7zdaemon/get-i7z-value", line 63, in get_temp_value
    print fmt % (temp if not args.in_fahrenheit else temp * (9.0/5.0) + 32, )
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 2: ordinal not in range(128)

Why is this only throwing an error when used from another program? Everything looks fine when I run it from a terminal directly.

Here's what I'm doing:

fmt = u"%d\u00B0C" if not args.in_fahrenheit else u"%d\u00B0F"
# ...
print fmt % (temp if not args.in_fahrenheit else temp * (9.0/5.0) + 32, )

That last line kills everything. What gives?

Upvotes: 2

Views: 764

Answers (2)

Martijn Pieters
Martijn Pieters

Reputation: 1124060

When you print unicode, the printed text has to be encoded to match whatever encoding is used for the output stream. For Linux terminals, for example, that is usually UTF-8 these days.

But when you use a different program to run your python script, the output stream encoding (a pipe, or a program window other than a terminal), may well use a different encoding. In this case, no encoding appears to be set and python then falls back to ASCII. The \u00B0 character cannot be encoded to ASCII and the encode fails.

Perhaps for this program, printing a lowercase o would suffice instead?

Or, the as the Conky FAQ seems to suggest, explicitly encode to Latin 1 (ISO 8859-1) to output the degree symbol:

print (fmt % (temp if not args.in_fahrenheit else temp * (9.0/5.0) + 32, )).encode('latin1')

And this posting seems to hint that encoding to UTF-8 is also an option (depending on Conky configuration)? Replace latin1 in the above example with utf8 to try that encoding instead.

Also see the Python Unicode HOWTO, which specifies how output encoding is handled.

Upvotes: 2

Alexander Koshkin
Alexander Koshkin

Reputation: 129

Try setting the coding in the top of your src file like this:

# coding: UTF-8

Upvotes: 0

Related Questions