By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,773 Members | 1,838 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,773 IT Pros & Developers. It's quick & easy.

Interpreting \ escape sequences in strings

P: n/a
How can I get the escapes from a command line parameter interpreted?

The user provides a string on the command line. The string might contain
traditional escapes such as \t, \n, etc. It might also contain escaped
octal or hex such as \001 or \x09.

The escapes are coming into sys.argv[] without shell interpretation. Do I
need to use the compile module to make this work? Any suggestions?

===
$ cat ./try_arglen2.py
#! /usr/bin/env python
import sys, StringIO
print sys.argv[1]

print sys.argv[1] % ()

sf = StringIO.StringIO()
print >> sf, sys.argv[1],
c = sf.getvalue()
sf.close()
print "now" + c + "is" + c + "the"

# This works because the interpreter is processing the escapes.

sf = StringIO.StringIO("\001")
c = sf.getvalue()
sf.close()
print "now" + c + "is" + c + "the"

===
$ ./try_arglen2.py '\001'
\001
\001
now\001is\001the
now?is?the
Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Paul Watson wrote:
How can I get the escapes from a command line parameter interpreted?

The user provides a string on the command line. The string might contain
traditional escapes such as \t, \n, etc. It might also contain escaped
octal or hex such as \001 or \x09.

The escapes are coming into sys.argv[] without shell interpretation. Do I
need to use the compile module to make this work? Any suggestions?

===
$ cat ./try_arglen2.py
#! /usr/bin/env python
import sys, StringIO
print sys.argv[1]

print sys.argv[1] % ()

sf = StringIO.StringIO()
print >> sf, sys.argv[1],
c = sf.getvalue()
sf.close()
print "now" + c + "is" + c + "the"

# This works because the interpreter is processing the escapes.

sf = StringIO.StringIO("\001")
c = sf.getvalue()
sf.close()
print "now" + c + "is" + c + "the"

===
$ ./try_arglen2.py '\001'
\001
\001
now\001is\001the
now?is?the


If I'm understanding you correctly:

<args.py>
import sys
print sys.argv[1].decode("string_escape")
</args.py>

$ python args.py "winter\nof\012our\x0Adiscontent"
winter
of
our
discontent

Peter

Jul 18 '05 #2

P: n/a

"Peter Otten" <__*******@web.de> wrote in message
news:c3*************@news.t-online.com...
Paul Watson wrote:
How can I get the escapes from a command line parameter interpreted?

The user provides a string on the command line. The string might contain traditional escapes such as \t, \n, etc. It might also contain escaped
octal or hex such as \001 or \x09.

The escapes are coming into sys.argv[] without shell interpretation. Do I need to use the compile module to make this work? Any suggestions?

===
$ cat ./try_arglen2.py
#! /usr/bin/env python
import sys, StringIO
print sys.argv[1]

print sys.argv[1] % ()

sf = StringIO.StringIO()
print >> sf, sys.argv[1],
c = sf.getvalue()
sf.close()
print "now" + c + "is" + c + "the"

# This works because the interpreter is processing the escapes.

sf = StringIO.StringIO("\001")
c = sf.getvalue()
sf.close()
print "now" + c + "is" + c + "the"

===
$ ./try_arglen2.py '\001'
\001
\001
now\001is\001the
now?is?the


If I'm understanding you correctly:

<args.py>
import sys
print sys.argv[1].decode("string_escape")
</args.py>

$ python args.py "winter\nof\012our\x0Adiscontent"
winter
of
our
discontent

Peter


I did have not explained it clearly. I want the user to specify a string
that I will put between words in the output. The user specified string can
have escape sequences. For example, the user wants to put a binary 1 (\001)
between each output word.

import sys
words = ['now', 'is', 'the', 'time']
print '\001'.join(words) #this works
print sys.argv[1].join(words) #this fails

$ ./putbetween.py '\001'
now?is?the?time
now\001is\001the\001time
Jul 18 '05 #3

P: n/a
Paul Watson wrote:
I did have not explained it clearly. I want the user to specify a string
Seems it was clear enough, you only didn't recognize the answer :-)
that I will put between words in the output. The user specified string
can
have escape sequences. For example, the user wants to put a binary 1
(\001) between each output word.

import sys
words = ['now', 'is', 'the', 'time']
print '\001'.join(words) #this works
print sys.argv[1].join(words) #this fails


Change the above line to

print sys.argv[1].decode("string_escape")

s.decode("string_escape") returns a new string with all c-style escape
sequences converted into the corresponding characters. This is an abuse -
ahem, example of a general mechanism. Look for codecs if you want to learn
more about it.

Peter

Jul 18 '05 #4

P: n/a

"Peter Otten" <__*******@web.de> wrote in message
news:c3*************@news.t-online.com...
Paul Watson wrote:
I did have not explained it clearly. I want the user to specify a
string
Seems it was clear enough, you only didn't recognize the answer :-)
that I will put between words in the output. The user specified string
can
have escape sequences. For example, the user wants to put a binary 1
(\001) between each output word.

import sys
words = ['now', 'is', 'the', 'time']
print '\001'.join(words) #this works
print sys.argv[1].join(words) #this fails


Change the above line to

print sys.argv[1].decode("string_escape")

s.decode("string_escape") returns a new string with all c-style escape
sequences converted into the corresponding characters. This is an abuse -
ahem, example of a general mechanism. Look for codecs if you want to learn
more about it.

Peter


Thank you. I appreciate your help. Yes, I missed it. I will look at the
decode doc. I expected that this was for converting character encodings
(codepages). This does work under Python 2.3, and decode was available in
2.2.

However, I am in a Python 2.1 environment. Do you know of any techniques
that would work under Python 2.1?
Jul 18 '05 #5

P: n/a
Paul Watson wrote:
However, I am in a Python 2.1 environment. Do you know of any techniques
that would work under Python 2.1?


eval('"' + s + '"')

This of course requires that " chars occuring in s are preceded by a
backslash:
def unescape(s): .... return eval('"' + s + '"')
.... unescape("\\x0a") '\n' unescape("\\x0a'") "\n'" unescape("\\x0a\"") Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 2, in unescape
File "<string>", line 1
"\x0a""
^
SyntaxError: invalid token unescape('\\x0a\\"')

'\n"'

Peter

Jul 18 '05 #6

P: n/a
Peter Otten wrote:
Paul Watson wrote:
However, I am in a Python 2.1 environment. Do you know of any techniques
that would work under Python 2.1?


eval('"' + s + '"')


I should have warned you that this is a security hole, as it allows the user
to execute arbitrary code. E. g:

<args.py>
import sys

def somefunc():
print "somefunc called"
return ""

def unescape(s):
return eval('"' + s + '"')

print unescape(sys.argv[1])
</args.py>

$ python args.py '"+somefunc()+"'
somefunc called

Peter

Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.