if opt[‘predictCSV‘] == ‘‘ :TypeError: ‘Namespace‘ object is not subscriptable

Suppose our code is as follows:

 
 

pythonCopy code

import argparse parser = argparse.ArgumentParser() parser.add_argument('--train', type=str, help='training data path') args = parser.parse_args() if args['train'] == '': print('Training data path is missing!')

The mistake here is to use args as a dictionary, but in fact args is a Namespace object that does not support dictionary operations. The correct way is to use the dot notation to access the properties of args:

 
 

pythonCopy code

import argparse parser = argparse.ArgumentParser() parser.add_argument('--train', type=str, help='training data path') args = parser.parse_args() if args.train == '': print('Training data path is missing!')

Guess you like

Origin blog.csdn.net/ihateright/article/details/131127968