将字典作为参数传递给另一个python脚本
我想将字典作为参数传递给单独的python脚本,如下所示:将字典作为参数传递给另一个python脚本,python,dictionary,arguments,Python,Dictionary,Arguments,我想将字典作为参数传递给单独的python脚本,如下所示: dict1= {'appid':'facebook'} subprocess.call('python mypython.py -o' + dict1, shell=True) (Also tried with os.system) mypython.py的内容: parser = argparse.ArgumentParser(fromfile_prefix_chars='@') parser.add_argument("-o",
dict1= {'appid':'facebook'}
subprocess.call('python mypython.py -o' + dict1, shell=True) (Also tried with os.system)
mypython.py的内容:
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
parser.add_argument("-o", dest="dict1")
args = parser.parse_args()
if args.dict1:
op1 = args.dict1
print op1
回溯详细信息:
python parent.py
Traceback (most recent call last):
File "parent.py", line 52, in <module>
sys.exit(main())
File "mypython.py", line 30, in main
subprocess.call('python mypython.py -o' + dict1, shell=True)
TypeError: cannot concatenate 'str' and 'dict' objects
它在:之后11之前添加了一个额外的空格,因此失败。唯一的解决方法是我的输入是字符串:
dict1="{2:11}"
os.system('python mypython.py -o '+dict1)
在上述情况下,它可以正常工作
有人能建议我如何将字典作为参数传递给另一个脚本吗?您可以将dict转换为字符串strdict1,然后在另一个类中使用进行解析。为什么要通过子进程进行此操作?看起来您正试图找到一种解决方法,解决如何从文件B调用文件a中的函数的问题。请。这是解决你假定问题的更好办法。
dict1="{2:11}"
os.system('python mypython.py -o '+dict1)