Python 如何将已解析语句列表转换为可在if语句中使用的列表或字符串?

Python 如何将已解析语句列表转换为可在if语句中使用的列表或字符串?,python,tensorflow,parsing,image-recognition,Python,Tensorflow,Parsing,Image Recognition,我使用的是来自的教程imagenet图像识别代码 我已经设法使一切正常工作,但我想知道如何将参数作为列表或字符串而不是解析的参数,以便可以使用普通的if命令 def main(_): maybe_download_and_extract() image = (FLAGS.image_file if FLAGS.image_file else os.path.join(FLAGS.model_dir, 'cropped_panda.jpg')) run_infer

我使用的是来自的教程imagenet图像识别代码

我已经设法使一切正常工作,但我想知道如何将参数作为列表或字符串而不是解析的参数,以便可以使用普通的if命令

def main(_):
  maybe_download_and_extract()
  image = (FLAGS.image_file if FLAGS.image_file else
           os.path.join(FLAGS.model_dir, 'cropped_panda.jpg'))
  run_inference_on_image(image)


if __name__ == '__main__':
  parser = argparse.ArgumentParser()
  # classify_image_graph_def.pb:
  #   Binary representation of the GraphDef protocol buffer.
  # imagenet_synset_to_human_label_map.txt:
  #   Map from synset ID to a human readable string.
  # imagenet_2012_challenge_label_map_proto.pbtxt:
  #   Text representation of a protocol buffer mapping a label to synset ID.
  parser.add_argument(
      '--model_dir',
      type=str,
      default='/tmp/imagenet',
      help="""\
      Path to classify_image_graph_def.pb,
      imagenet_synset_to_human_label_map.txt, and
      imagenet_2012_challenge_label_map_proto.pbtxt.\
      """
  )
  parser.add_argument(
      '--image_file',
      type=str,
      default='',
      help='Absolute path to image file.'
  )
  parser.add_argument(
      '--num_top_predictions',
      type=int,
      default=5,
      help='Display this many predictions.'
  )
  #how do i get a variable that i can interact with from this

  FLAGS, unparsed = parser.parse_known_args()
  tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)
我绝对没有解析方面的经验,因此非常感谢您的帮助。

EDIT Afetr我看到的注释
ArgumentParser
。如果插入方法
parser.parse_args
,则返回一个名称空间。现在您可以访问它的属性,并且可以获得用户传递的值

#Every parameter can be accessed using namespace.parameter_name, for example
# with namepsace.model_dir, and you get the string inserted by the user

namespace = parser.parse_args()
if namespace.verbose:
    print("Verbose: ", + str(verbose))
如果要迭代所有属性,可以使用中所述的字典。从字典传递到列表就很容易了


旧答案 对于解析输入参数,我使用
getopt
。最难理解的部分是如何指定参数和可选参数,但并不难

getopt
将返回一个参数列表,您可以对其迭代并应用条件。(请参见,也适用于python 3.6和2)。我举一个例子:

def main():
    options, remainder = getopt.getopt(sys.argv[1:], 'tci:', ['train', 'classify', 'id'])
    for opt, arg in options:
        #This is a bool optional parameter
        if opt in ('-t', '--train'):
            train = True

        #This is a bool optional parameter
        elif opt in ('-c', '--classify'):
            predict = True

        #This is an integer required parameter
        elif opt in ('-i', '--id'):
            id= arg

    if train:
        funtion1()
    elif predict:
        function2(id)

if __name__ == '__main__':
    main()
文件说:

getopt.getopt(args、shortopts、longopts=[]) 解析命令行选项和参数列表
args
是要分析的参数列表,没有对正在运行的程序的前导引用。通常,这意味着
sys.argv[1:][/code>shortopts是脚本想要识别的选项字母字符串,其中需要一个参数,后跟冒号(“:”;即Unix getopt()使用的相同格式)longopts如果指定,则必须是字符串列表,其中包含应支持的长选项的名称。选项名称中不应包含前导“--”字符。需要参数的长选项后面应该跟一个等号('=')


请注意,用户可以将任何他想要的作为参数,您需要检查它是否正确。

很抱歉,我甚至不知道原始代码是如何工作的,更不用说如何更改了。你能给我更多的信息吗?这是我正在使用的代码,等待进一步检查,这不是我想要的。我知道这会返回用户设置的参数,但我想返回解析器返回的东西,即imagenet做出的预测。你的帮助将非常感激-你似乎知道你在做什么。