Python: "AttributeError: 'Namespace' object has no attribute" argparse

Dav*_*atu 3 python

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)
Run Code Online (Sandbox Code Playgroud)

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'
Run Code Online (Sandbox Code Playgroud)

However, if i change from

df_confusion.to_html(args.output-html)
Run Code Online (Sandbox Code Playgroud)

To

df_confusion.to_html(args.output)
Run Code Online (Sandbox Code Playgroud)

It works as it should. Can anyone explain why it doesn't work, and how can i make it work with args.output-html?

Dee*_*ace 5

默认情况下(即,如果您不提供destkwarg add_argument),由于Python属性不能包含字符(事实上​​它们可以包含字符,但是只能使用来访问),因此创建属性时它会更改-为。_-getattr

这意味着你应该改变args.output-htmlargs.output_html,并args.output-csvargs.output_csv