How do I pass an array of strings to a python script as an argument?
Asked Answered
C

1

-1

I've written a swift app that outputs an array of strings.

I would like to import this array into a python script for further processing into an excel file via xlsxwriter, I would like to do this as an argument.

My array looks like this:

[["1", "12:32", "Harry\'s\na wizard", "", ""], ["2", "12:34", "Harry reads a sign:", "Sign:", "\"You're a wizard Harry\""]]

I'd like to pass this into python verbatim, so I can process it into an excel table. The output is a human readable file.

I've tried adding my array into PyChars's "Modify Run Configuration...", then processing it via:

import sys
arr = sys.argv[1]
print(arr)

but I get: [[1,

I try to add the argument as """argument""", but I get: [[1, 12:32, Harry's\na

I try as: f"""argument""", but get: f[[1, 12:32, Harry's\na

f'argument' results in: f'[[1,

I try reading the argument with:

arr = ast.literal_eval(sys.argv)

but I get several errors ending in: "ValueError: malformed node or string: ..."

arr = ast.literal_eval(sys.argv[1])

gives me: return compile(source, filename, mode, flags, File "", line 1 [[1, ^ SyntaxError: unexpected EOF while parsing

I've solved this problem by exporting the array to a JSON file from my swift app and importing it in the python script, but I'd really like to know if there's any way of passing it as a command line argument.

Concerted answered 8/12, 2022 at 8:55 Comment(0)
O
1

You need to quote the argument in the shell to make it treat it as a single argument. And since the argument contains embedded quotes, you need to escape them, as well as the embedded backslash characters.

You'd also need to escape any $ or ` characters if they existed, since they also have meaning in double-quoted strings.

python testargv.py "[[\"1\", \"12:32\", \"Harry's\\na wizard\", \"\", \"\"], [\"2\", \"12:34\", \"Harry reads a sign:\", \"Sign:\", \"\\\"You're a wizard Harry\\\"\"]]"

Then use ast.literal_eval(sys.argv[1]) in the script to convert this to a list.

You can't use single quotes around the whole argument, because it doesn't support escaping of embedded single quotes. Another way to avoid all this escaping hell is to switch between single and double-quoted strings. Wrap most of it in single quotes, but use "'" for the embedded single quotes.

python testargv.py '[["1", "12:32", "Harry'"'"'s\na wizard", "", ""], ["2", "12:34", "Harry reads a sign:", "Sign:", "\"You'"'"'re a wizard Harry\""]]'
Offhand answered 8/12, 2022 at 18:13 Comment(4)
Hey thanks a lot for explaining this, I was worried that might be the case. Now I just have to find a way to create the fully escaped array programmatically in Swift.Concerted
Assuming I manage to get my array to look like this, how do I make python interpret it as an array without it splitting along commas that are actually within the text?Concerted
Commas inside Python quotes won't be treated as delimiters. E.g. if you change "12:32" to "12,32" it will be a literal comma.Offhand
All these quoting issues are only a problem if the command is being executed by the shell. If you're calling the Python script from another program, it should have a way of executing the script directly without using the shell. Analogous to Python's subprocess.run().Offhand

© 2022 - 2024 — McMap. All rights reserved.