Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing newline within string into a python script from the command line

I have a script that I run from the command line which I would like to be able to pass string arguments into. As in

script.py --string "thing1\nthing2"

such that the program would interpret the '\n' as a new line. If string="thing1\nthing2" I want to get

print string

to return:

thing1
thing2

rather than thing1\nthing2

If I simply hard-code the string "thing1\nthing2" into the script, it does this, but if it's entered as a command line argument via getopt, it doesn't recognize it. I have tried a number of approaches to this: reading in the cl string as r"%s" % arg, various ways of specifying it on the commandline, etc, and nothing seems to work. Ideas? Is this completely impossible?

like image 240
Astro_Dart Avatar asked Oct 22 '14 21:10

Astro_Dart


People also ask

Can you put \n in a string?

Adding Newline Characters in a String In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.

How do you add a line break in string?

How Can I Add a New String Line? The newline character is \n in JavaScript and many other languages. All you need to do is add \n character whenever you require a line break to add a new line to a string.

How do you print a newline variable in Python?

Use the addition operator to print a new line after a variable, e.g. print(variable + '\n') . The newline ( \n ) character is a special character in python and is used to insert new lines in a string.

How do you add N in Python?

Just use \n ; Python automatically translates that to the proper newline character for your platform.


2 Answers

From https://stackoverflow.com/a/4918413/478656 in Bash, you can use:

script.py --string $'thing1\nthing2'

e.g.

$ python test.py $'1\n2'
1
2

But that's Bash-specific syntax.

like image 128
TessellatingHeckler Avatar answered Oct 24 '22 04:10

TessellatingHeckler


This is really a shell question since the shell does all the command parsing. Python doesn't care what's happening with that and only gets what comes through in the exec system call. If you're using bash, it doesn't do certain kinds of escaping between double quotes. If you want things like \n, \t, or \xnn to be escaped, the following syntax is a bash extension:

python test.py $'thing1\nthing2'

Note that the above example uses single quotes and not double quotes. That's important. Using double quotes causes different rules to apply. You can also do:

python test.py "thing1
thing2"

Here's some more info on bash quoting if you're interested. Even if you're not using bash, it's still good reading:

http://mywiki.wooledge.org/Quotes

like image 33
David Sanders Avatar answered Oct 24 '22 04:10

David Sanders