interpretation of special characters in Python

T

TP

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
 
P

Peter Pearson

$ python -c "print '\033[30;44m foo \033[0m'"
[writes an escape sequence to stdout]
$ echo -e $esc$ColorBlackOnDarkblue foo $esc$ColorReset
[also writes an escape sequence to stdout]
$ echo -n $esc$ColorBlackOnDarkblue foo $esc$ColorReset
\033[30;44m foo \033[0m

[snip, shuffle]
$ export esc="\033"
$ export ColorBlackOnDarkblue="[30;44m"
$ export ColorReset="[0m"

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) [snip]
$ python color.py
\033[30;44m foo \033[0m

The string "\033" is 4 characters long. Your shell variable
"esc" is 4 characters long. Your Python program prints
those four characters. You want it to re-interpret those 4
characters into a single escape character.

One of this group's regular participants can (I hope) tell
us three breathtakingly elegant ways to do that. I'm sorry
I can't.

When you run echo, it recognizes the 4-character "esc" as a
convention for representing a single character, and performs
the re-interpretation for you. When you tell python
"print '\033[30;44m foo \033[0m'", python interprets
the "\033" as a single character.
 
T

TP

Dennis said:
Off-hand, I'd probably try first with:

csi = "\033["

and then define your

colorblackondarkblue = $csi"30;44m"

Thanks for your answer.
I have tried this slight modification, but it does not change anything on my
terminal.
 
T

TP

Peter Pearson wrote:

Thanks for your answer.
When you run echo, it recognizes the 4-character "esc" as a
convention for representing a single character, and performs
the re-interpretation for you. When you tell python
"print '\033[30;44m foo \033[0m'", python interprets
the "\033" as a single character.

So, the python print command *can* interpret these 4-character as a single
character. It would be odd if there were no possibility to do the same
thing when the characters are (i) stored in a python variable, or (ii) come
from the environment variables. Does anybody know any way to re-interpret a
string in Python? I have tried to play with "eval" (as in bash), but it
does not yield anything.
 
T

TP

TP said:
So, the python print command *can* interpret these 4-character as a single
character. It would be odd if there were no possibility to do the same
thing when the characters are (i) stored in a python variable

Sorry, it works when using variables. Try for example:

col="[0;31m"
esc="\033"
colreset="[0m"
print esc + col + "foobar" + esc + colreset
 
P

Peter Otten

Peter said:
$ python -c "print '\033[30;44m foo \033[0m'"
[writes an escape sequence to stdout]
$ echo -e $esc$ColorBlackOnDarkblue foo $esc$ColorReset
[also writes an escape sequence to stdout]
$ echo -n $esc$ColorBlackOnDarkblue foo $esc$ColorReset
\033[30;44m foo \033[0m

[snip, shuffle]
$ export esc="\033"
$ export ColorBlackOnDarkblue="[30;44m"
$ export ColorReset="[0m"

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) [snip]
$ python color.py
\033[30;44m foo \033[0m

The string "\033" is 4 characters long. Your shell variable
"esc" is 4 characters long. Your Python program prints
those four characters. You want it to re-interpret those 4
characters into a single escape character.

One of this group's regular participants can (I hope) tell
us three breathtakingly elegant ways to do that. I'm sorry
I can't.

When you run echo, it recognizes the 4-character "esc" as a
convention for representing a single character, and performs
the re-interpretation for you. When you tell python
"print '\033[30;44m foo \033[0m'", python interprets
the "\033" as a single character.

Peter Pearson's explanation is spot-on. You get the 4-character sequence
instead of the escape code chr(27).

$ export esc="\033"
$ python
Python 2.5.1 (r251:54863, Mar 7 2008, 03:39:23)
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import os
os.environ["esc"] '\\033'
print _
\033

If you want to interpret "\\033" as "\033" you have to perform the
conversion explicitly. Fortunately there already is an encoding that
understands these escape sequences for characters:
esc = os.environ["esc"].decode("string-escape")
esc '\x1b'
print "%s[30;44malles so schoen bunt hier%s[0m" % (esc, esc)
alles so schoen bunt hier

Peter
 
T

TP

Peter said:
esc = os.environ["esc"].decode("string-escape")
esc '\x1b'
print "%s[30;44malles so schoen bunt hier%s[0m" % (esc, esc)
alles so schoen bunt hier

Thanks a lot for your help. It works perfectly.
Indeed, one can read in the documentation concerning encodings:

"Produce a string that is suitable as string literal in Python source code"
 
P

Peter Pearson

TP said:
So, the python print command *can* interpret these 4-character as a single
character. It would be odd if there were no possibility to do the same
thing when the characters are (i) stored in a python variable

Sorry, it works when using variables. Try for example:

col="[0;31m"
esc="\033"
colreset="[0m"
print esc + col + "foobar" + esc + colreset

I don't understand exactly what you mean by "Sorry", but
let me direct your attention to the fact that the "interpretation"
step about which we're talking happens during the parsing
of the string literal (i.e., while the statement esc="\033" is
processed), not during the execution of the print statement.
To prove this assertion, simply print len(esc). You will see
that esc is a single character long.
 
T

TP

Peter said:
I don't understand exactly what you mean by "Sorry"

I means: please forgive me for having said that it does not work with
variables, because it is completely false.

Thanks one more time

Julien
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top