'How we can add a double quotes to list of arguments from command line to python code

Can anyone please help me how to add double quotes to multiple arguments passed from a CLI to python code

ASK :

I am passing a list of argument to a single parameter and at the python code I need to add "" double quotes to all the value passed from the arguments

At the CLI I am passing, for e.g. value1=[a,b,c,d]

At the python code I am using below code to get all the parameter of the value1

value1=[varibaleName.args.value1]

But while passing/interpreting these values I need to add a double quotes to all the value lets say for e.g. it should be treated like below

value1=["a","b","c","d"]

So when I pass the argument at CLI, it should be interpreted as in double quotes while assigning those values to variable/list.

Please let me know if the ask in unclear to you and need your help ! Appreciate for fast response

Need a code how to add double quotes to all value passed to a list argument from CLI



Solution 1:[1]

The double-quotes are ignored by the interpreter. You can encase the string in single quotes.
Using test script:

#!/usr/bin/python

import sys

print ('Number of arguments:', len(sys.argv), 'arguments.')
print ('Argument List:', str(sys.argv))

And running in the terminal as follows:

python args.py '"a"' '"b"' '"c"'

gives output with double quotes as follows:

Number of arguments: 4 arguments.
Argument List: ['args.py', '"a"', '"b"', '"c"']

You can also escape the double-quotes as follows

python args.py \"a\" \"b\" \"c\"
Number of arguments: 4 arguments.
Argument List: ['args.py', '"a"', '"b"', '"c"']

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1