473,847 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is the best way to handle a command line argument that includes an escape sequence like \n?

Joe
I'm using Python 2.4 on Windows XP SP2.

I'm trying to receive a command line argument that is a newline (\n)

Here is the command line to use

sample.py "\n"

Here is a sample.py script

import sys

c = sys.argv[1]

# when run c is set to \\n instead of \n.

I created a test batch file

echo %1

to confirm that it was not the cmd.exe command processor causing the issue.

It appears that Python treats the comand line string as a raw string.

Is this a bug? If not what is the best way to work around the issue?

Obviously I could use a hack

if c == '\\n':
c = '\n'

But surely there is a better way.

NOTE that I used \n in my sample but I also want to support other escape
sequences too.

Jul 18 '05 #1
8 3459
Joe wrote:
It appears that Python treats the comand line string as a raw string.

what is the best way to work around the issue?


You probably want to use str.decode with the encoding 'string_escape'[1]

py> s = r'\n\t'
py> s
'\\n\\t'
py> s.decode('strin g_escape')
'\n\t'

STeVe

[1]http://docs.python.org/lib/standard-encodings.html
Jul 18 '05 #2
Joe
Steve,

THANKS! That is exactly what I was looking for but unable to find.

Joe

"Steven Bethard" <st************ @gmail.com> wrote in message
news:oc******** ************@co mcast.com...
Joe wrote:
It appears that Python treats the comand line string as a raw string.

what is the best way to work around the issue?


You probably want to use str.decode with the encoding 'string_escape'[1]

py> s = r'\n\t'
py> s
'\\n\\t'
py> s.decode('strin g_escape')
'\n\t'

STeVe

[1]http://docs.python.org/lib/standard-encodings.html

Jul 18 '05 #3
Joe wrote:
I'm using Python 2.4 on Windows XP SP2.

I'm trying to receive a command line argument that is a newline (\n)

Here is the command line to use

sample.py "\n"

Here is a sample.py script

import sys

c = sys.argv[1]

# when run c is set to \\n instead of \n.

I created a test batch file

echo %1

to confirm that it was not the cmd.exe command processor causing the issue.

It appears that Python treats the comand line string as a raw string.

Is this a bug? If not what is the best way to work around the issue?

Obviously I could use a hack

if c == '\\n':
c = '\n'

But surely there is a better way.

NOTE that I used \n in my sample but I also want to support other escape
sequences too.
I don't want you getting more confused rather than less - newcomers are
sometimes confused by Python's encoding of backslashes and such when
printing out strings. What is your evidence for the assertion that c is
set to \\n rather than \n?

In Unix, it's easier to avoid this, since the shell lets you enter
multi-line strings (note that these lines may wrap in the mail):

[sholden@headrat sholden]$ python -c "import sys; print
repr(sys.argv[1])" "\n"
'\\n'
[sholden@headrat sholden]$ python -c "import sys; print repr(sys.argv[1])" " "

'\n'

The first case simply demonstrates that the shell doesn't interpret
backslash escape sequences. I used repr() because that's what the
interpreter will print if you enter an expression at the interactive
prompt. The first case shows that the argument is two characters - you
can verify this using the len() function.

The second case shows how (with a sensible command shell) you can
provide a newline as an argument.

In Windows we can emulate the first case exactly:
C:\Steve>C:\pyt hon24\python -c "import sys; print repr(sys.argv[1])" "\n"
'\\n'

Unfortunately Windows XP's command interpreter doesn't bother to wait
until you close a double-quote left open at the end of a line:

C:\Steve>\pytho n24\python -c "import sys; print repr(sys.argv[1])" "
''

So someone else will have to tell you how to do that, but you should be
clear about what's happening before you try and correct the problem.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #4
Op 2005-03-02, Joe schreef <Jo********@hot mail.com>:
I'm using Python 2.4 on Windows XP SP2.

I'm trying to receive a command line argument that is a newline (\n)

Here is the command line to use

sample.py "\n"
Are you sure this supplies a newline and not the string <backslach> <n>
Here is a sample.py script

import sys

c = sys.argv[1]

# when run c is set to \\n instead of \n.

I created a test batch file

echo %1

to confirm that it was not the cmd.exe command processor causing the issue.


I'm not sure this confirms anything. IMO it is possible that echo
will translate <backslach> <n> to <newline>, giving you the
impression that you have provideded a newline on the command line
while in fact you have not.

--
Antoon Pardon
Jul 18 '05 #5
Joe
Hi Steve,

I've been using Python for many years, just hadn't had to deal with an
escape sequence in the command line args. :-) and couldn't find the solution
in the docs.

It is easy to prove my assertion:

import sys
c = sys.argv[1]
print len(c)
print 'Line 1', c, 'Line 2'

Output:
2
Line 1 \n Line 2

Versus:

import sys
c = sys.argv[1]
print len(c)
print 'Line 1', c.decode('strin g_escape'), 'Line 2'

Output:
2
Line 1
Line 2

Agreed, it is much easier on Unix to deal with this, I have not seen a way
to get the XP command interpreter to allow a multiline command line as you
described.

Your solution was exactly what I need. I had an escape sequence entered on
the command line and needed to decode the string so that Python used it as
an escape sequence, in fact the sequence really is part of the output that
the program produces.

Thanks again for your assistance.

Regards,

Joe
I don't want you getting more confused rather than less - newcomers are
sometimes confused by Python's encoding of backslashes and such when
printing out strings. What is your evidence for the assertion that c is
set to \\n rather than \n?

In Unix, it's easier to avoid this, since the shell lets you enter
multi-line strings (note that these lines may wrap in the mail):

[sholden@headrat sholden]$ python -c "import sys; print repr(sys.argv[1])"
"\n"
'\\n'
[sholden@headrat sholden]$ python -c "import sys; print repr(sys.argv[1])"
"
"

'\n'

The first case simply demonstrates that the shell doesn't interpret
backslash escape sequences. I used repr() because that's what the
interpreter will print if you enter an expression at the interactive
prompt. The first case shows that the argument is two characters - you can
verify this using the len() function.

The second case shows how (with a sensible command shell) you can provide
a newline as an argument.

In Windows we can emulate the first case exactly:
C:\Steve>C:\pyt hon24\python -c "import sys; print repr(sys.argv[1])" "\n"
'\\n'

Unfortunately Windows XP's command interpreter doesn't bother to wait
until you close a double-quote left open at the end of a line:

C:\Steve>\pytho n24\python -c "import sys; print repr(sys.argv[1])" "
''

So someone else will have to tell you how to do that, but you should be
clear about what's happening before you try and correct the problem.

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/

Jul 18 '05 #6
Joe
Antoon,

I tested the batch file :-)

The one line batchfile does prove it because it prints out <backslash><n >
and not <newline>.

See other post, decode is exactly what was needed to fix the problem.

Regards,

Joe

"Antoon Pardon" <ap*****@forel. vub.ac.be> wrote in message
news:sl******** ************@rc pc42.vub.ac.be. ..
Op 2005-03-02, Joe schreef <Jo********@hot mail.com>:
I'm using Python 2.4 on Windows XP SP2.

I'm trying to receive a command line argument that is a newline (\n)

Here is the command line to use

sample.py "\n"


Are you sure this supplies a newline and not the string <backslach> <n>
Here is a sample.py script

import sys

c = sys.argv[1]

# when run c is set to \\n instead of \n.

I created a test batch file

echo %1

to confirm that it was not the cmd.exe command processor causing the
issue.


I'm not sure this confirms anything. IMO it is possible that echo
will translate <backslach> <n> to <newline>, giving you the
impression that you have provideded a newline on the command line
while in fact you have not.

--
Antoon Pardon

Jul 18 '05 #7
Joe wrote:
Hi Steve,

I've been using Python for many years, just hadn't had to deal with an
escape sequence in the command line args. :-) and couldn't find the solution
in the docs.

It is easy to prove my assertion:

import sys
c = sys.argv[1]
print len(c)
print 'Line 1', c, 'Line 2'

Output:
2
Line 1 \n Line 2
The "2" appears to prove *my* assertion that the argument passed by the
shell to your Python program consists of 2 characters, "\\" followed by "n".
Versus:

import sys
c = sys.argv[1]
print len(c)
print 'Line 1', c.decode('strin g_escape'), 'Line 2'

Output:
2
Line 1
Line 2
As does this.
Agreed, it is much easier on Unix to deal with this, I have not seen a way
to get the XP command interpreter to allow a multiline command line as you
described.

Your solution was exactly what I need. I had an escape sequence entered on
the command line and needed to decode the string so that Python used it as
an escape sequence, in fact the sequence really is part of the output that
the program produces.
In fairness it was Steven Bethard's solution that gave you the solution
you needed. As long as ytour problem is solved, that's fine, and it
appears that you've solved it in a reasonably cross-platform way.
Thanks again for your assistance.

Always happy to help when I can!

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Jul 18 '05 #8
Joe
Hey no fair changing last names in the middle of a thread :-)

Thanks to BOTH Steve's.

In fairness it was Steven Bethard's solution that gave you the solution
you needed. As long as ytour problem is solved, that's fine, and it
appears that you've solved it in a reasonably cross-platform way.

Steve Holden http://www.holdenweb.com/

Jul 18 '05 #9

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

Similar topics

33
9889
by: Frank | last post by:
What is the best IDE for developing PHP applications? What do you use and why? Thanks.
37
2881
by: middletree | last post by:
Yesterday, I posted a problem which, by the way, I haven't been able to solve yet. But in Aaron's reply, he questioned why I did several things the way I did. My short answer is that I have a lot to learn, but now I'd like to ask anyone who reads this, including Aaron, for some clarification. I imagine others might benefit, too. "Aaron Bertrand - MVP" <aaron@TRASHaspfaq.com> wrote > A few suggestions. > (3) why do you constantly set...
121
10237
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
3
1866
by: Herb Finkel | last post by:
The following code is fairly simple to capture command line arguments: using System; namespace ConsoleApplication1 { class Class1 { static void Main(string args) { string first = args;
7
2840
by: mackmelo | last post by:
Hi, I'm trying to create a program that reads these two characters (& and ^) as arguments to a funcion. However since they are part of the operational system characters with special funcions when in an argumento to a function, whenever I put them in my list of arguments, I get errors and can't parse them to my program. Any Ideas on how to use them inside my application? Ah, and i don't want to use other arguments before them so it...
2
2765
by: Milan | last post by:
Hi, Please guide me how to set command line argument and how to retrive command line argument. Senario: vb.net application should be able to execute from command prompt by passing login and password and should be able to execute process form (with his parameter) User should not be able to see GUI
10
2053
by: Chad | last post by:
Given: #include <stdio.h> #include <stdlib.h> #define MAXLINE 200 int main(void) { char buff;
13
2702
by: Pieter Edelman | last post by:
Hi, I'm currently writing a command-line program in Python, which takes commands in the form of: ../myprog.py ARGS So pretty standard stuff. In my case, ARGS is a list of image files. One of the possible options is to specify a file holding information about the photos. You'd specify it with (in this particular case) the - t switch, and you can specify multiple files by repeating this switch:
8
3081
by: mdh | last post by:
Hi all, I have a file, whose path is: "/Users/m/k&R/test_file" How do I include the '&' in a string constant? ( I need this for the example on p162). I have tried to use the Hex notation x26, as in "/Users/m/k\x26R/test_file".
0
9886
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10706
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10338
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9482
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7882
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7056
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5719
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5911
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3164
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.