[GRASS-dev] g.message and unicode in python

Glynn Clements glynn at gclements.plus.com
Fri Nov 21 10:17:36 PST 2014


Nikos Alexandris wrote:

> How can I print an informational message, using g.message, that 
> contains unicode stuff via python (2.x)?
> 
> For example, how do I correctly translate this command
> 
> g.message message="ρ(p) = π x L(λ) x d^2 / ESUN(λ) x cos(θ(S))"
> 
> (it contains the greek r (ρ, pronounced rho) and pi (π, pronounced pe)
> 
> to work in a python script?

In general, you can't assume that the terminal is capable of
displaying those characters.

If they are representable in the current locale, then

	encoding = locale.getdefaultlocale()[1]
	grass.message(msg.encode(encoding))

should pass the correct bytes.

If they aren't representable, then the .encode() method will raise a
UnicodeEncodeError.

If a UTF-8 locale is being used, any Unicode string can be converted,
but that doesn't mean that the characters will display correctly; if
the font lacks those characters, they'll just show as "?" or "~" or a
box with the hex code or similar.

-- 
Glynn Clements <glynn at gclements.plus.com>


More information about the grass-dev mailing list