Python 如何使用optpass解析自定义字符串?

Python 如何使用optpass解析自定义字符串?,python,optparse,Python,Optparse,如何使用optpass而不是命令行参数解析自定义字符串? 我想解析使用raw\u input()获得的字符串。 如何使用OptPass进行此操作?首先使用拆分输入 >>> import shlex >>> shlex.split(raw_input()) this is "a test" of shlex ['this', 'is', 'a test', 'of', 'shlex'] 首先使用拆分器拆分输入 >>> import shlex

如何使用optpass而不是命令行参数解析自定义字符串?

我想解析使用
raw\u input()
获得的字符串。 如何使用OptPass进行此操作?

首先使用拆分输入

>>> import shlex
>>> shlex.split(raw_input())
this is "a test" of shlex
['this', 'is', 'a test', 'of', 'shlex']
首先使用拆分器拆分输入

>>> import shlex
>>> shlex.split(raw_input())
this is "a test" of shlex
['this', 'is', 'a test', 'of', 'shlex']

optparse
需要一个以shell样式分解的值列表(这就是
argv[1://code>的含义)。要完成以字符串开头的相同操作,请尝试以下操作:

parser = optparse.OptionParser()
# Set up your OptionParser

inp = raw_input("Enter some crap: ")

try: (options, args) = parser.parse_args(shlex.split(inp))
except:
    # Error handling.
parse_args
的可选参数是在转换后的字符串中替换的参数


请注意,
shlex.split
parse_args
都可能出现异常。当您处理来自用户的输入时,最好同时考虑这两种情况。

optparse
需要一个以shell样式分解的值列表(这就是
argv[1://code>的含义)。要完成以字符串开头的相同操作,请尝试以下操作:

parser = optparse.OptionParser()
# Set up your OptionParser

inp = raw_input("Enter some crap: ")

try: (options, args) = parser.parse_args(shlex.split(inp))
except:
    # Error handling.
parse_args
的可选参数是在转换后的字符串中替换的参数


请注意,
shlex.split
parse_args
都可能出现异常。处理用户输入时,最好同时考虑这两种情况。

我需要解析输入。。在把它列成一个列表之后,我该怎么做呢?我需要解析输入。。在把它列成一个列表之后,我该怎么做呢?