-1

I would like to create a python script and parse a list of command-line arguments.

Specifically, I would like to give it the following arguments and have it read them as an array. Something like this:

run script.py (['file1.ascii', float_12, float_13, float_14, float_15, float_16], \
               ['file2.ascii', float_22, float_23, float_24, float_25, float_26] )

where the input array is:

input_array = (['file1.ascii', float_12, float_13, float_14, float_15, float_16], \
               ['file2.ascii', float_22, float_23, float_24, float_25, float_26] )

and inside the script, I want to do a "for loop" and and assign the variables in this way:

for i in range(0, len(input_array) ): 
 variable_1 = 'filei.ascii'
 variable_2 = float_i2
 variable_3 = float_i3
 variable_4 = float_i4
 variable_5 = float_i5
 variable_6 = float_i6
 #do my calculations
Baron Grouda
  • 103
  • 1
  • 5
  • ...and what part of this do you have a question about? – Charles Duffy Aug 19 '21 at 17:27
  • 2
    Get out of the habit of using `for i in range(list)):`, use `for item in list:` instead. – Barmar Aug 19 '21 at 17:28
  • 2
    Mind, a command line argument list is just a flat array of strings. You **cannot** pass anything else there -- no nested arrays, no non-string datatypes -- without needing to convert to and from a flat array of strings. – Charles Duffy Aug 19 '21 at 17:28
  • In python, the command line arguments are put in the array `sys.argv`. Is that what you're looking for? – Barmar Aug 19 '21 at 17:29
  • 1
    That said, if each inner array is always exactly six items, then you just split your larger array into smaller 6-item arrays, convert all but the first element in each such inner array to a float, and you're done. A narrower question that describes where you're having a problem would be a better place to start. – Charles Duffy Aug 19 '21 at 17:31
  • 1
    I'd like to discourage this non-standard use of args in the format of a Python list/tuple. It's much more common to use classical flags and individual arguments or accept a structure from a file in a standard format like JSON. A bit more information about your use case is nice so we can help you avoid an [xy problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). It's also an antipattern to use `variable_n` -- use a _list_, accessed with `lst[n]`. See [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) – ggorlen Aug 19 '21 at 17:31
  • 1
    (note that `run script.py (["string here", 1.23], ["another string", 4.56])` isn't even valid shell syntax, so it's not something you can actually do) – Charles Duffy Aug 19 '21 at 17:32

1 Answers1

1

You can convert the array to JSON.

In the calling script, do:

import json, subprocess

args = json.dumps(
    ['file1.ascii', float_12, float_13, float_14, float_15, float_16],
    ['file2.ascii', float_22, float_23, float_24, float_25, float_26] 
)
subprocess.run(['python', 'script.py', args])

and in script.py you parse the JSON

import json, sys

input_array = json.loads(sys.argv[1])
for filename, var1, var2, var3, var4, var5 in input_array:
    # do stuff
Barmar
  • 741,623
  • 53
  • 500
  • 612