473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

optparse escaping control characters

optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something? Can this be prevented, or worked around?

This behaviour doesn't occur with non-control characters.

For example, if this program (called test.py):
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-d", dest="delimiter", action="store")
(options, args) = parser.parse_args()
print options

is run as follows:
python test.py -d '\t'

it outputs:
{'delimiter': '\\t'}

i.e. the \t has had an escape character added to give \\t.

Aug 19 '08 #1
7 4093
wa**********@gmail.com writes:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something? Can this be prevented, or worked around?
It has nothing to do with optparse, it's how Python prints strings:

$ python -c 'import sys; print sys.argv' '\t'
['-c', '\\t']

Note that you're not really passing a control character to Python,
you're passing a two-character string consisting of \ and t. When
representing the string inside a data structure, Python escapes the \
to avoid confusion with a real control character such as \t.

If you try printing the string itself, you'll see that everything is
correct:

$ python -c 'import sys; print sys.argv[1]' '\t'
\t
Aug 19 '08 #2
wa**********@gmail.com wrote:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something?
you're missing the distinction between the content of a string object,
and how the corresponding string literal looks.
>>x = {'delimiter': '\\t'}
x
{'delimiter': '\\t'}
>>x["delimiter"]
'\\t'
>>print x["delimiter"]
\t
>>len(x["delimiter"])
2

</F>

Aug 19 '08 #3
On Aug 19, 1:45*pm, Hrvoje Niksic <hnik...@xemacs.orgwrote:
wannymaho...@gmail.com writes:
optparse seems to be escaping control characters that I pass as
arguments on the command line. *Is this a bug? *Am I missing
something? *Can this be prevented, or worked around?

It has nothing to do with optparse, it's how Python prints strings:

$ python -c 'import sys; print sys.argv' '\t'
['-c', '\\t']

Note that you're not really passing a control character to Python,
you're passing a two-character string consisting of \ and t. *When
representing the string inside a data structure, Python escapes the \
to avoid confusion with a real control character such as \t.

If you try printing the string itself, you'll see that everything is
correct:

$ python -c 'import sys; print sys.argv[1]' '\t'
\t
Thanks for the reply, much clearer now, just one more question. How
would I pass a control character to python on the command line?

Aug 19 '08 #4
On Aug 19, 10:35 pm, wannymaho...@gmail.com wrote:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing
something? Can this be prevented, or worked around?

This behaviour doesn't occur with non-control characters.

For example, if this program (called test.py):
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-d", dest="delimiter", action="store")
(options, args) = parser.parse_args()
print options

is run as follows:
python test.py -d '\t'

it outputs:
{'delimiter': '\\t'}

i.e. the \t has had an escape character added to give \\t.
You are inputting a TWO-byte string composed of a backslash and a
lowercase t, and feeding that to OptionParser.

C:\junk>type test.py
import sys; a = sys.argv[1]; d = {'delimiter': a}
print len(a), a, str(a), repr(a)
print d

# Note: this is Windows, where the shell quote is ", not '
C:\junk>python test.py "\t"
2 \t \t '\\t'
{'delimiter': '\\t'}

The extra backslash that you see is caused by the (implicit) use of
repr() to display the string.

If you want/need to enter a literal TAB character in the command line,
consult the manual for your shell.

HTH,
John
Aug 19 '08 #5
On Tue, 19 Aug 2008 05:35:27 -0700, wannymahoots wrote:
optparse seems to be escaping control characters that I pass as
arguments on the command line. Is this a bug? Am I missing something?
Can this be prevented, or worked around?
You are misinterpreting the evidence. Here's the short explanation:

optparse isn't escaping a control character, because you're not supplying
it with a control character. You're supplying it with two normal
characters, which merely *look* like five (including the quote marks)
because of Python's special handling of backslashes.
If you need it, here's the long-winded explanation.

I've made a small change to your test.py file to demonstrate:

# test.py (modified)
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-d", dest="delimiter", action="store")
(options, args) = parser.parse_args()
print "Options:", options
print "str of options.delimiter =", str(options.delimiter)
print "repr of options.delimiter =", repr(options.delimiter)
print "len of options.delimiter =", len(options.delimiter)
Here's what it does when I call it:

$ python test.py -d '\t'
Options: {'delimiter': '\\t'}
str of options.delimiter = \t
repr of options.delimiter = '\\t'
len of options.delimiter = 2
When you pass '\t' in the command line, the shell sends a literal
backslash followed by a lowercase t to Python. That is, it sends the
literal string '\t', not a control character.

Proof: pass the same string to the "wc" program using "echo". Don't
forget that echo adds a newline to the string:

$ echo 't' | wc # just a t
1 1 2
$ echo '\t' | wc # a backslash and a t, not a control character
1 1 3
That's the first half of the puzzle. Now the second half -- why is Python
adding a *second* backslash to the backslash-t? Actually, it isn't, but
it *seems* to be adding not just a second backslash but also two quote
marks.

The backslash in Python is special. If you wanted a literal backslash t
in a Python string, you would have to type *two* backslashes:

'\\t'

because a single backslash followed by t is escaped to make a tab
character.

But be careful to note that even though you typed five characters (quote,
backslash, backslash, t, quote) Python creates a string of length two: a
single backslash and a t.

Now, when you print something using the str() function, Python hides all
that complexity from you. Hence the line of output that looks like this:

str of options.delimiter = \t

The argument is a literal backslash followed by a t, not a tab character.

But when you print using the repr() function, Python shows you what you
would have typed -- five characters as follows:

repr of options.delimiter = '\\t'

But that's just the *display* of a two character string. The actual
string itself is only two characters, despite the two quotes and the two
backslashes.

Now for the final piece of the puzzle: when you print most composite
objects, like the OptParse Value objects -- the object named "options" in
your code -- Python prints the internals of it using repr() rather than
str().

--
Steven
Aug 19 '08 #6
Thanks for all the responses!
Aug 19 '08 #7
Dan Halligan <da**********@gmail.comwrites:
How would I pass a control character to python on the command line?
It depends on which command line you are using. Most Unix-like shells
will allow you to input a control character by preceding it with ^V.
Since \t is the TAB character, you should be able to input it like
this:

$ python -c 'import sys; print sys.argv' '^V<tab>'
['-c', '\t'] # note single backslash
Aug 19 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records....
5
by: Henry | last post by:
I have this simple code, string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" ); string input = @"a&+" + "\"" + @"@(-d)\e"; Regex re = new Regex( string.Format(@"(+)", escaped),...
11
by: Geoff Caplan | last post by:
Hi folks, The thread on injection attacks was very instructive, but seemed to run out of steam at an interesting point. Now you guys have kindly educated me about the real nature of the issues,...
4
by: agarwalpiyush | last post by:
Hello, I am going nuts with trying to get the following to work: This is what I intend to do: I have a line in /etc/syslog.conf which I need to delete based on ip-address provided to me in a...
4
by: James Black | last post by:
I have an application that relies on JSON, and I realized yesterday that I when the user types at least some of these characters (most likely all, haven't had time to verify) that the parser has...
4
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using XslCompiledTransform to do the trnasform (namely the output...
3
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only...
4
by: Mathias Waack | last post by:
We've integrated python into a legacy application. Everything works fine (of course because its python;). There's only one small problem: the application reads the commandline and consumes all...
1
by: Mariano Mara | last post by:
Hi everyone. I'm building a script with optparse. One of the parameters will be a password. How can I code the optparse to accept/handle/format the password so that the user does not have to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.