简体   繁体   English

Python:“ AttributeError:'Namespace'对象没有属性” argparse

[英]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.

相关问题 Python ArgParse AttributeError:“ str”对象没有属性 - Python ArgParse AttributeError: 'str' object has no attribute Argparse using subcommands/subparsers — AttributeError: 'Namespace' object has no attribute - Argparse using subcommands/subparsers — AttributeError: 'Namespace' object has no attribute AttributeError: 'Namespace' 对象没有属性 - AttributeError: 'Namespace' object has no attribute 使用 Argparse 时,“AttributeError: 'Namespace' object has no attribute 'command'” 不断出现在终端上 - While using Argparse “AttributeError: 'Namespace' object has no attribute 'command'” keeps showing up on terminal AttributeError:'Namespace'对象没有属性'check' - AttributeError: 'Namespace' object has no attribute 'check' AttributeError:“命名空间”object 没有属性“rmvApp” - AttributeError: 'Namespace' object has no attribute 'rmvApp' AttributeError:“命名空间”object 没有属性“myFile” - AttributeError: 'Namespace' object has no attribute 'myFile' AttributeError:“名称空间”对象没有属性“ URL” - AttributeError: 'Namespace' object has no attribute 'url' AttributeError:命名空间 object 没有属性“累积” - AttributeError: namespace object has no attribute 'accumulate' AttributeError-命名空间对象没有属性用户名 - AttributeError - Namespace object has no attribute username
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM