相关疑难解决方法(0)

将Python argparse.Namespace()作为字典处理的正确方法是什么?

如果我想使用一个需要字典或类似映射的对象的方法(参见collections.Mapping)来使用argparse.ArgumentParser()作为Namespace对象的结果,那么正确的方法是什么?

C:\>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
>>> args = argparse.Namespace()
>>> args.foo = 1
>>> args.bar = [1,2,3]
>>> args.baz = 'yippee'
>>> args['baz']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'Namespace' object has no attribute '__getitem__'
>>> dir(args)
['__class__', '__contains__', '__delattr__', '__dict__', '__doc__', '__eq__', '_ …
Run Code Online (Sandbox Code Playgroud)

python dictionary duck-typing

201
推荐指数
2
解决办法
11万
查看次数

如何迭代参数

我有这样的剧本:

import argparse

parser = argparse.ArgumentParser(
                description='Text file conversion.'
                )
parser.add_argument("inputfile",   help="file to process", type=str)
parser.add_argument("-o", "--out", default="output.txt",
                    help="output name")
parser.add_argument("-t", "--type", default="detailed",
                    help="Type of processing")

args = parser.parse_args()

for arg in args:
    print(arg)
Run Code Online (Sandbox Code Playgroud)

但它不起作用.我收到错误:

TypeError: 'Namespace' object is not iterable
Run Code Online (Sandbox Code Playgroud)

如何迭代参数及其值?

python argparse

45
推荐指数
3
解决办法
3万
查看次数

为什么我收到此错误: TypeError: 'Namespace' object is not subscriptable

我面临 argparse 库的问题。

我有encodings.pickle 文件,并且我按照下面的代码来识别演员,但加载嵌入似乎不起作用。

这是代码:

ap = argparse.ArgumentParser()
ap.add_argument("-e", "--encodings", required=True,
                help="path to serialized db of facial encodings")
ap.add_argument("-i", "--image", required=True,
                help="path to input image")
ap.add_argument("-d", "--detection-method", type=str, default="cnn",
                help="face detection model to use: either `hog` or `cnn`")
ap.add_argument("-fnn", "--fast-nn", action="store_true")
args = parser.parse_args('')
print(args)

# load the known faces and embeddings
print("[INFO] loading encodings...")
data = pickle.loads(open(args["encodings"], "rb").read())
Run Code Online (Sandbox Code Playgroud)

源代码可以在这里找到。

python

12
推荐指数
1
解决办法
2万
查看次数

标签 统计

python ×3

argparse ×1

dictionary ×1

duck-typing ×1