0

I am trying to pass a string with spaces and newlines in python script called main.py as a command line argument but am not sure if it is possible.

I am trying to pass the string '5 2\n 3 3 2\n 1 1 2' and have it appear within the script exactly like that. For this I am using sys and argv as such:

Command line:

python main.py "5 2\n 3 3 2\n 1 1 2"

Python script:

info_input = sys.argv[1]

However it appears that the output to this is a list with added escape characters in the newlines is:

['5 2\\n 3 3 2\\n 1 1 2']

Is it possible to pass this as an argument and have the output within the python appear as:

"5 2\n 3 3 2\n 1 1 2"

Many Thanks

EDIT

print(info_input)

'5 2\n 3 3 2\n 1 1 2'

input_split = info_input.split(sep='\n')
print(input_split)

['5 2\\n 3 3 2\\n 1 1 2']

It hasn't split on the newlines here and is a list of the whole thing.

ffigari
  • 431
  • 6
  • 18
geds133
  • 1,503
  • 5
  • 20
  • 52
  • What are you looking to achieve? It's correct for the `\\n` to be there. Note that if you do `print(sys.argv[1])` the string is printed as you want. – ffigari Nov 25 '20 at 13:13
  • My script uses the format `"5 2\n 3 3 2\n 1 1 2"` and splits the input by `\n` below which seems to fail if the escape characters are introduced. The output is also a list which is not in the string format that my script requires to process. – geds133 Nov 25 '20 at 13:29
  • Do you want to upload an example of how that split you mention is failing? Can´t you use another separator that is not the newline character? The `\n` should be "splitable" anyway, maybe something like this can help https://stackoverflow.com/a/24237675/2923526 ? – ffigari Nov 25 '20 at 13:37
  • (I mean the usage of `splitlines`) – ffigari Nov 25 '20 at 13:38
  • @ffigari Apologies for the delay. The splitlines method seems to have worked far better than splitting on the `\n` using the `sep` argument. However due to the escape character it still does not split anything and is the same as the edited version above. – geds133 Nov 25 '20 at 16:19
  • I left an answer with some clarifications. In the future I suggest leaving a minimum working example of your problem. `working` in the sense that I could copy and paste the code to run it, since that makes it easier to debug the problem. – ffigari Nov 25 '20 at 17:25

2 Answers2

1

A couple of clarifications regarding this two points of your question

I am trying to pass a string with spaces and newlines

Is it possible to pass this as an arguement and have the output within the python appear as:

When you call your script with python main.py "5 2\n 3 3 2\n 1 1 2" you are not passing actual newline characters. This is why you get \\n in your string, Python is escaping those \n since otherwise it would mean that your string does have newline chars.
You are confusing the representation of the string and the string itself. Check this question about repr vs str.
When printing the string alone it is printed nicely, but when printing the list the escaped characters are shown, which explains why you get different results.

When you do:

input_split = info_input.split(sep='\n')
print(input_split)  

you are not actually splitting your string, since you string does not contain newline characters (\n), it contains escaped newline characters (\\n).
If you actually want to split your string in newlines you could do:

input_split = info_input.split(sep='\\n')
print(input_split)  

which outputs ['5 2', ' 3 3 2', ' 1 1 2'].


That said, if your goal is to have actual newline characters in your program you can replace the escaped newlines with newlines:

import sys

info_input = sys.argv[1]
info_input_with_escaped_newlines = info_input
print("info_input_with_escaped_newlines as string", info_input_with_escaped_newlines)
print("info_input_with_escaped_newlines as list", [info_input_with_escaped_newlines])

info_input_with_newlines = info_input.replace('\\n', '\n')
print("info_input_with_newlines as string", info_input_with_newlines)
print("info_input_with_newlines as list", [info_input_with_newlines])

which outputs

> python as.py "5 2\n 3 3 2\n 1 1 2"
info_input_with_escaped_newlines as string 5 2\n 3 3 2\n 1 1 2
info_input_with_escaped_newlines as list ['5 2\\n 3 3 2\\n 1 1 2']
info_input_with_newlines as string 5 2
 3 3 2
 1 1 2
info_input_with_newlines as list ['5 2\n 3 3 2\n 1 1 2']

Notice how now split does split the string:

import sys

info_input = sys.argv[1].replace('\\n', '\n').split(sep='\n')
print(info_input)

which outputs:

python as.py "5 2\n 3 3 2\n 1 1 2"
['5 2', ' 3 3 2', ' 1 1 2']
ffigari
  • 431
  • 6
  • 18
0

A possible bypass could be string.replace(). You could do '5 2NL 3 3 2NL 1 1 2' and then info_input = sys.argv[1].replace("NL", "\n"). This might be a temporary solution until you find something better.