interpretation of special characters in Python

TP Tribulations at Paralleles.invalid
Sun Jul 6 17:42:26 EDT 2008


Hi everybody,

I am new to Python, I try to understand how Python treats special
characters. For example, if I execute the following line in a shell
console, I obtain a colored string:

$ python -c "print '\033[30;44m foo \033[0m'"

So, it works.
Some time ago, I have made a lot of shell variables with all possible colors
(with shell functions executed each time I launch a new shell). For
example:

$ echo -e $esc$ColorBlackOnDarkblue foo $esc$ColorReset

gives the same result than the python command above.
To know the corresponding non-interpreted characters, I can use the -n
option of echo:

$ echo -n $esc$ColorBlackOnDarkblue foo $esc$ColorReset
\033[30;44m foo \033[0m

So, it is exactly the string above, as expected.

My problem arises when it comes to get these shell variables ( $esc,
$ColorBlackOnDarkblue, $ColorReset) in a Python script, with os.environ, in
the following 5-line script:

import os
Color = os.environ['ColorBlackOnDarkblue']
ColorReset = os.environ['ColorReset']
Esc = os.environ['esc']
print '%s%s%s%s%s' % (Esc, Color, " foo ", Esc, ColorReset)

Run this script color.py, but after having defined these shell variables in
a shell:

$ export esc="\033"
$ export ColorBlackOnDarkblue="[30;44m"
$ export ColorReset="[0m"

When I execute the Python script, I do not obtain any special character
interpretation in Python:

$ python color.py
\033[30;44m foo \033[0m

Why? What is the problem? Is there any solution?
I really want to get my shell color variables.

Thanks a lot

-- 
TP (Tribulations Parallèles)

"Allez, Monsieur, allez, et la foi vous viendra." (D'Alembert).



More information about the Python-list mailing list