Search code examples
python-3.xargparseargv

how to pass command args to python scripts when the args contains the Single quotes and Double quotation marks?


import sys
import json

if __name__ == '__main__':
    print(sys.argv)
    print(json.loads(sys.argv[1]))

result:

(env) λ python main.py '["br_V1R22C00RR1_bugfix"]'
['main.py', "'[br_V1R22C00RR1_bugfix]'"]
Traceback (most recent call last):
  File "C:\Users\x\PycharmProjects\GaussClientUpgrade\main.py", line 30, in <module>
    print(json.loads(sys.argv[1]))
  File "c:\users\x\appdata\local\programs\python\python39\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "c:\users\x3\appdata\local\programs\python\python39\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "c:\users\x\appdata\local\programs\python\python39\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I want to pass a string like '["br_V1R22C00RR1_bugfix"]', do not eliminate the ' and ", it's a json.dumps(list)'s result, i use json.loads to load the '["br_V1R22C00RR1_bugfix"]'.

but how can I pass the string i want?


Solution

  • This problem has no relation with the program language, and e.g. can happen in java too.

    It's because of different argument handling between bash and dos.

    In linux:

    [root@clarence lib]# python test.py \"['br']\"
    "[br]"
    [br]
    [root@clarence lib]# python test.py '["br"]'
    ["br"]
    ['br']
    [root@clarence lib]# python test.py [\"br\"]
    ["br"]
    ['br']
    [root@clarence lib]# python test.py "[\"br\"]"
    ["br"]
    ['br']
    [root@clarence lib]# python test.py "['br']"
    ['br']
    Traceback (most recent call last):
      File "test.py", line 7, in <module>
        print(json.loads(sys.argv[1]))
      File "/usr/lib64/python3.7/json/__init__.py", line 348, in loads
        return _default_decoder.decode(s)
      File "/usr/lib64/python3.7/json/decoder.py", line 337, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/usr/lib64/python3.7/json/decoder.py", line 355, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
    [root@clarence lib]# python test.py "[\"br\"trer't]"
    ["br"trer't]
    Traceback (most recent call last):
    
    

    In windows dos:

    (env) λ python test.py \"['br']\"
    "['br']"
    ['br']
    (env) λ python test.py '["br"]'
    '[br]'
    Traceback (most recent call last):
      File "C:\Users\clarence\Desktop\test.py", line 7, in <module>
        print(json.loads(sys.argv[1]))
    (env) λ python test.py [\"br\"]
    ["br"]
    ['br']
    (env) λ python test.py "[\"br\"]"
    ["br"]
    ['br']
    (env) λ python test.py "['br']"
    ['br']
    Traceback (most recent call last):
    

    python test.py '["br"]' in bash can run successfully, but not in dos.