简体   繁体   中英

Python subprocess using JSON dictionary as argument

I am trying to pass a JSON dictionary as an argument through the terminal using a subprocess . The dictionary keeps ending up different from when I pass it manually through the terminal. I am using this code for the subprocess :

subprocess.call("python ../power_supply_gui/PowerSupplyControl.py "+ "{\"CHANNEL\":\"d\",\"VOLT\":\"1\",\"CURRENTLIMIT\":\"1\",\"ENABLE\":\"1\"}",shell=True)

I should be getting this when I read it in the other program:

{"CHANNEL":"d","VOLT":"1","CURRENTLIMIT":"1","ENABLE":"1"}

but instead, I am getting this:

{CHANNEL:d,VOLT:1,CURRENTLIMIT:1,ENABLE:1}

This is how the program that is called is reading the argument and outputting it:

print sys.argv[1]
print type(sys.argv[1])
commandDictionary=json.loads(sys.argv[1]) 

The conversion of the dictionary to a json string puts a space after the colon which means that before the space and after the space are split up into different arguments. If you want this to work try removing the spaces with str(dictionary).replace(" ","") as the argument.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM