[英]Python: “AttributeError: 'Namespace' object has no attribute” argparse
My code looks like this: 我的代码如下所示:
parser.add_argument('-i', '--input', help='Input path/to/file.csv', required=True)
parser.add_argument('-oh', '--output-html', help='Output path/to/confusion_matrix.html', required=True)
parser.add_argument('-oc', '--output-csv', help='Output path/to/confusion_matrix.csv', required=True)
args = parser.parse_args()
....
y_true = pd.Series(true_data, name="Actual")
y_pred = pd.Series(pred_data, name="Predicted")
df_confusion = pd.crosstab(y_true, y_pred)
df_confusion.to_html(args.output-html)
df_confusion.to_csv(args.output-csv)
When i try to run it, it gives me this error: 当我尝试运行它时,它给了我这个错误:
df_confusion.to_html(args.output-html)
AttributeError: 'Namespace' object has no attribute 'output'
However, if i change from 但是,如果我从
df_confusion.to_html(args.output-html)
To 至
df_confusion.to_html(args.output)
It works as it should. 它可以正常工作。 Can anyone explain why it doesn't work, and how can i make it work with args.output-html? 谁能解释为什么它不起作用,以及如何使它与args.output-html一起使用?
By default (ie if you don't provide dest
kwarg to add_argument
) it changes -
to _
when creating the attribute since Python attributes can't contain the character -
(as a matter of fact they can, but then they are only accessible by using getattr
). 默认情况下(也就是说,如果你不提供dest
kwarg到add_argument
)它的变化-
以_
创建时的属性因为Python属性不能包含的字符-
实际上他们可以的事情,但他们只能通过访问使用getattr
)。
It means that you should change args.output-html
to args.output_html
, and args.output-csv
to args.output_csv
. 这意味着您应该将args.output-html
更改为args.output_html
,并将args.output-csv
更改为args.output_csv
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.