I'm trying following example :
python -c "import sys; print sys.argv[1]" "test\ntest"
Output :
test\ntest
But I want following
test
test
Update 1#
As @devnull suggested can solve the problem if user itself pass the with $
but how about python should take care of this ?
I've tried :
python -c "import sys; print '$' + sys.argv[1]" 'test\ntest'
but output :
$test\ntest
This occurs because, according to the Python Documentation: The default value of the end parameter of the built-in print function is \n , so a new line character is appended to the string. 💡 Tip: Append means "add to the end".
In Python, the new line character “\n” is used to create a new line. When inserted in a string all the characters after the character are added to a new line. Essentially the occurrence of the “\n” indicates that the line ends here and the remaining characters would be displayed in a new line.
"\r\n" is the default Windows style for line separator. "\r" is classic Mac style for line separator.
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.
As deed02392 mentioned, shell sends the characters literally, so python "escapes" the backslash internally. To see what I mean, try
python -c "import sys; print repr(sys.argv[1])" "test\ntest"
'test\\ntest'
To work around this, do:
python -c "import sys; print sys.argv[1].decode('string_escape')" "test\ntest"
test
test
Tell the shell to pass what you want to see:
python -c "import sys; print sys.argv[1]" $'test\ntest'
This would produce:
test
test
The $'string'
syntax is referred to as ANSI-C Quoting.
Another way would be to tell the shell to interpret your original output:
python -c "import sys; print sys.argv[1]" 'test\ntest' | xargs -0 echo -en
This would also produce:
test
test
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With