如何将Python脚本的参数读入字典

如何将Python脚本的参数读入字典,python,dictionary,split,argv,Python,Dictionary,Split,Argv,我正在尝试创建Python脚本,将参数读入字典,然后打印“发送方ip”的值 使用以下命令调用脚本: python lookup.py "email=joe@aol.com, sender-ip=10.10.10.10" 这是剧本 from sys import argv script, input = argv attributeMap = {} delimiter = "=" for item in input: if delimiter in item: tu

我正在尝试创建Python脚本,将参数读入字典,然后打印“发送方ip”的值

使用以下命令调用脚本:

python lookup.py "email=joe@aol.com, sender-ip=10.10.10.10"
这是剧本

from sys import argv

script, input = argv

attributeMap = {}
delimiter = "="
for item in input:

    if delimiter in item:
        tuple = item.split(delimiter)
        print tuple[0]
        print tuple[1]
        attributeMap[tuple[0]]= tuple[1]
        print attributeMap[tuple[0]]

print attributeMap['sender-ip']

输入
不是列表;它是一根绳子。您需要首先将该字符串拆分为多个项目:

items = input.split(', ')
for item in items:
请注意,还有一个名为
input()
的内置函数,请尽量不要使用该名称并屏蔽该内置函数

以下是根据您的输入生成字典的一些代码:

import sys

argument = sys.argv[1]
attr_map = dict(item.strip().split('=', 1) for item in argument.split(','))
print attr_map['sender-ip']

输入
不是列表;它是一根绳子。您需要首先将该字符串拆分为多个项目:

items = input.split(', ')
for item in items:
请注意,还有一个名为
input()
的内置函数,请尽量不要使用该名称并屏蔽该内置函数

以下是根据您的输入生成字典的一些代码:

import sys

argument = sys.argv[1]
attr_map = dict(item.strip().split('=', 1) for item in argument.split(','))
print attr_map['sender-ip']
很明显,你指的是
“电子邮件=joe@aol.com,发送方ip=10.10.10.10“

下面是一个片段:

def toDict(s):
  result = {}
  for piece in s.split(','):
    # piece is like 'foo=bar' here
    key, value = piece.split('=')
    result[key.strip()] = value.strip() # .strip() removes spaces around
  return result
工作原理:

>>> arg_string = "email=joe@aol.com, sender-ip=10.10.10.10"
>>> toDict(arg_string)
{'email': 'joe@aol.com', 'sender-ip': '10.10.10.10'}
>>> _
很明显,你指的是
“电子邮件=joe@aol.com,发送方ip=10.10.10.10“

下面是一个片段:

def toDict(s):
  result = {}
  for piece in s.split(','):
    # piece is like 'foo=bar' here
    key, value = piece.split('=')
    result[key.strip()] = value.strip() # .strip() removes spaces around
  return result
工作原理:

>>> arg_string = "email=joe@aol.com, sender-ip=10.10.10.10"
>>> toDict(arg_string)
{'email': 'joe@aol.com', 'sender-ip': '10.10.10.10'}
>>> _

使用
docopt
很容易

安装了
docopt

$ pip install docopt
像这样重写您的解决方案
lookup.py

"""Usage: lookup.py <e-mail> <sender-ip>

Arguments:
  <e-mail>     e-mail address
  <sender-ip>  IP address

Sample use:

  $ python lookup.py john.doe@example.com 1.2.3.4
"""
from docopt import docopt

if __name__ == "__main__":
    args = docopt(__doc__)
    attributeMap = {"e-mail": args["<e-mail>"], "sender-ip": args["<sender-ip>"]}
    print attributeMap

使用
docopt
很容易

安装了
docopt

$ pip install docopt
像这样重写您的解决方案
lookup.py

"""Usage: lookup.py <e-mail> <sender-ip>

Arguments:
  <e-mail>     e-mail address
  <sender-ip>  IP address

Sample use:

  $ python lookup.py john.doe@example.com 1.2.3.4
"""
from docopt import docopt

if __name__ == "__main__":
    args = docopt(__doc__)
    attributeMap = {"e-mail": args["<e-mail>"], "sender-ip": args["<sender-ip>"]}
    print attributeMap

您的
发件人ip
条目使用
-
而不是
=
;打字错误?我已经在任何情况下纠正了它。不要隐藏内置的
input
函数。
argparse
模块设计用于解析命令行参数。是什么产生了这些论点?该字符串看起来有点太人类可读性,无法自动生成,并且
python lookup.py”电子邮件=joe@aol.com“发件人ip=10.10.10.10”
将更容易从
lookup.py
中处理。您的
发件人ip
条目使用
-
而不是
=
;打字错误?我已经在任何情况下纠正了它。不要隐藏内置的
input
函数。
argparse
模块设计用于解析命令行参数。是什么产生了这些论点?该字符串看起来有点太人类可读性,无法自动生成,并且
python lookup.py”电子邮件=joe@aol.com“发送方ip=10.10.10.10”
将更容易从
lookup.py中处理。定义“不工作”。你有例外吗?如果是这样,什么是回溯?让我看看你贴的新东西,然后我可以解释better@Glowie:我希望你的代码会有例外。@Martjin Pieters:我试过你的代码,它工作得很好!在我做其他导致值无法输出的事情之前。定义“不工作”。你有例外吗?如果是这样,什么是回溯?让我看看你贴的新东西,然后我可以解释better@Glowie:我希望你的代码会有例外。@Martjin Pieters:我试过你的代码,它工作得很好!在我做其他导致值无法输出的操作之前。当然,除非OP实际尝试从其他源解析字符串格式。当然,除非OP实际尝试从其他源解析字符串格式。