【问题标题】:error while converting command line arguments to a dictionary in python 2.7在 python 2.7 中将命令行参数转换为字典时出错
【发布时间】:2021-05-30 21:42:22
【问题描述】:

我正在尝试将 cmd 行参数转换为字典。我为此目的使用的代码

import sys

cmd_line_args = dict(arg.split('=') for arg in sys.argv[1:])

我在命令行中调用这个函数

python python_basics.py p1=param1 p2="param 2" p3=param3

但我遇到了一个错误

Traceback (most recent call last):
  File "python_basics.py", line 23, in <module>
    temp_dict = dict(arg.split('='))
ValueError: dictionary update sequence element #1 has length 6; 2 is required

我尝试将参数转换为字典的方式有什么问题吗?

【问题讨论】:

    标签: python python-2.7 dictionary


    【解决方案1】:

    dict 需要一个包含两项迭代的迭代

    所以,cmd_line_args = dict([arg.split('=') for arg in sys.argv[1:]]) 解决了我的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-02
      • 1970-01-01
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多