-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.

1 Answer 1

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\""]]'
Sign up to request clarification or add additional context in comments.

4 Comments

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.
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?
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.
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().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.