我正在尝试将字典设置为可选参数(使用 argparse);以下是我目前所拥有的:
parser.add_argument('-i','--image', type=dict, help='Generate an image map from the input file (syntax: {\'name\': <name>, \'voids\': \'#08080808\', \'0\': \'#00ff00ff\', \'100%%\': \'#ff00ff00\'}).')
但是运行脚本:
$ ./script.py -i {'name': 'img.png','voids': '#00ff00ff','0': '#ff00ff00','100%': '#f80654ff'}
script.py: error: argument -i/--image: invalid dict value: '{name:'
即使在解释器内部,
>>> a={'name': 'img.png','voids': '#00ff00ff','0': '#ff00ff00','100%': '#f80654ff'}
工作得很好。
那么我应该如何传递参数呢?提前致谢。
原文由 user975296 发布,翻译遵循 CC BY-SA 4.0 许可协议
Necroing this:
json.loads
也在这里工作。看起来并不太脏。退货:
{u'0': u'#ff00ff00', u'100%': u'#f80654ff', u'voids': u'#00ff00ff', u'name': u'img.png'}