Python 里边的parser用法

本文介绍了Python中用于解析命令行选项的optparse模块,包括术语解释、基本使用教程,以及如何定义和处理选项。通过示例展示了如何创建OptionParser实例、定义选项,并解析命令行参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2011-8-23

Python 里边的parser用法

 

15.5. optparse — Parser for command

line options

http://docs.python.org/library/optparse.html

 

Here’s an example of using optparse in a simple script:

from optparse import OptionParser
[...]
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")
 
(options, args) = parser.parse_args()

 

With these few lines of code, users of your script can now do the “usual thing” on the command-line, for example:

<yourscript> --file=outfile -q

 

Thus, the following command lines are all equivalent to the above example:

<yourscript> -f outfile --quiet
<yourscript> --quiet --file outfile
<yourscript> -q -foutfile
<yourscript> -qfoutfile

 

Additionally, users can run one of

<yourscript> -h
<yourscript> --help

and optparse will print out a brief summary of your script’s options:

Usage: <yourscript> [options]
 
Options:
  -h, --help            show this help message and exit
  -f FILE, --file=FILE  write report to FILE
  -q, --quiet           don't print status messages to stdout

where the value of yourscript is determined at runtime (normally fromsys.argv[0]).

15.5.1.1. Terminology

argument

a string entered on the command-line, and passed by the shell to execl() orexecv(). In Python, arguments are elements of sys.argv[1:] (sys.argv[0] is the name of the program being executed). Unix shells also use the term “word”.

It is occasionally desirable to substitute an argument list other thansys.argv[1:], so you should read “argument” as “an element of sys.argv[1:], or of some other list provided as a substitute for sys.argv[1:]“.

option

an argument used to supply extra information to guide or customize the execution of a program. There are many different syntaxes for options; the traditional Unix syntax is a hyphen (“-“) followed by a single letter, e.g. -x or -F. Also, traditional Unix syntax allows multiple options to be merged into a single argument, e.g. -x -F is equivalent to -xF. The GNU project introduced -- followed by a series of hyphen-separated words, e.g.--file or --dry-run. These are the only two option syntaxes provided byoptparse.

Some other option syntaxes that the world has seen include:

·   a hyphen followed by a few letters, e.g. -pf (this is not the same as multiple options merged into a single argument)

·   a hyphen followed by a whole word, e.g. -file (this is technically equivalent to the previous syntax, but they aren’t usually seen in the same program)

·   a plus sign followed by a single letter, or a few letters, or a word, e.g. +f, +rgb

·   a slash followed by a letter, or a few letters, or a word, e.g. /f,/file

These option syntaxes are not supported by optparse, and they never will be. This is deliberate: the first three are non-standard on any environment, and the last only makes sense if you’re exclusively targeting VMS, MS-DOS, and/or Windows.

option argument

an argument that follows an option, is closely associated with that option, and is consumed from the argument list when that option is. Withoptparse, option arguments may either be in a separate argument from their option:

-f foo
--file foo

or included in the same argument:

-ffoo
--file=foo

Typically, a given option either takes an argument or it doesn’t. Lots of people want an “optional option arguments” feature, meaning that some options will take an argument if they see it, and won’t if they don’t. This is somewhat controversial, because it makes parsing ambiguous: if -atakes an optional argument and -b is another option entirely, how do we interpret -ab? Because of this ambiguity, optparse does not support this feature.

positional argument

something leftover in the argument list after options have been parsed, i.e. after options and their arguments have been parsed and removed from the argument list.

required option

an option that must be supplied on the command-line; note that the phrase “required option” is self-contradictory in English. optparse doesn’t prevent you from implementing required options, but doesn’t give you much help at it either.

For example, consider this hypothetical command-line:

prog -v --report /tmp/report.txt foo bar

-v and --report are both options. Assuming that --report takes one argument,/tmp/report.txt is an option argument. foo and bar are positional arguments.

15.5.2. Tutorial

While optparse is quite flexible and powerful, it’s also straightforward to use in most cases. This section covers the code patterns that are common to any optparse-based program.

First, you need to import the OptionParser class; then, early in the main program, create an OptionParser instance:

from optparse import OptionParser
[...]
parser = OptionParser()

Then you can start defining options. The basic syntax is:

parser.add_option(opt_str, ...,
                  attr=value, ...)

Each option has one or more option strings, such as -f or --file, and several option attributes that tell optparse what to expect and what to do when it encounters that option on the command line.

Typically, each option will have one short option string and one long option string, e.g.:

parser.add_option("-f", "--file", ...)

You’re free to define as many short option strings and as many long option strings as you like (including zero), as long as there is at least one option string overall.

The option strings passed to add_option() are effectively labels for the option defined by that call. For brevity, we will frequently refer to encountering an option on the command line; in reality, optparse encounters option strings and looks up options from them.

Once all of your options are defined, instruct optparse to parse your program’s command line:

(options, args) = parser.parse_args()

(If you like, you can pass a custom argument list to parse_args(), but that’s rarely necessary: by default it uses sys.argv[1:].)

parse_args() returns two values:

  • options, an object containing values for all of your options—e.g. if --filetakes a single string argument, then options.file will be the filename supplied by the user, or None if the user did not supply that option
  • args, the list of positional arguments leftover after parsing options

This tutorial section only covers the four most important option attributes:actiontypedest (destination), and help. Of these, action is the most fundamental.

OK,that is enough for me to realize the GNU RADIO’s code. If I have extra time, I will continue to learn it.

<div class="basicInfo_Dxt9K J-basic-info"><dl class="basicInfoBlock_zx4H_ left"><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">本    名</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">胡歌</span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">外文名</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">Hu Ge</span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">性    别</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">男</span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">民    族</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">汉族</span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">国    籍</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true"><a class="innerLink_oGMKn" href="/item/%E4%B8%AD%E5%9B%BD/22516505?fromModule=lemma_inlink" target="_blank" data-from-module="basicInfo">中国</a></span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">出生地</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">上海市</span><span class="text_H18Us" data-text="true"><a class="innerLink_oGMKn" href="/item/%E5%BE%90%E6%B1%87%E5%8C%BA/937282?fromModule=lemma_inlink" target="_blank" data-from-module="basicInfo">徐汇区</a></span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">出生日期</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">1982年9月20日</span><span>(壬戌年八月初四)</span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">毕业院校</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true"><a class="innerLink_oGMKn" href="/item/%E4%B8%8A%E6%B5%B7%E6%88%8F%E5%89%A7%E5%AD%A6%E9%99%A2/1736818?fromModule=lemma_inlink" target="_blank" data-from-module="basicInfo">上海戏剧学院</a></span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">星    座</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true"><a class="innerLink_oGMKn" href="/item/%E5%A4%84%E5%A5%B3%E5%BA%A7/2859614?fromModule=lemma_inlink" target="_blank" data-from-module="basicInfo">处女座</a></span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">血    型</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">O型</span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">身    高</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">185 cm</span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">体    重</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true">70 kg</span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">经纪公司</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true"><a class="innerLink_oGMKn" href="/item/%E5%A4%A9%E6%B4%A5%E5%94%90%E4%BA%BA%E5%BD%B1%E8%A7%86%E8%82%A1%E4%BB%BD%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8/19977007?fromModule=lemma_inlink" target="_blank" data-from-module="basicInfo">天津唐人影视股份有限公司</a></span></dd></div><div class="itemWrapper_ZNZh3"><dt class="basicInfoItem_zB304 itemName_LS0Jv">代表作品</dt><dd class="basicInfoItem_zB304 itemValue_AYbkR"><span class="text_H18Us" data-text="true"><a class="innerLink_oGMKn" href="/item/%E4%BB%99%E5%89%91%E5%A5%87%E4%BE%A0%E4%BC%A0/5130936?fromModule=lemma_inlink" target="_blank" data-from-module="basicInfo">仙剑奇侠传</a>这是页面html源码中基本信息部分的部分代码请重新帮我生成代码
05-23
用python根据需求完成一个TXT解析器的简单开发 一 修改说明: 需求一: 一开始说要解析UECapabilityInfo 消息里的supportedBandCombination-r10 这个IE里的CA组合转化成易阅读的表现形式. 我以为一组CA组合就是一组: bandEUTRA-r10 ca-BandwidthClassUL-r10 ca-BandwidthClassDL-r10 supportedMIMO-CapabilityDL-r10 功能实现: 有效信息筛选:于是就用循环把UECapabilityInformation的数据里每一行作为一个元素放到list里面 然后用bandEUTRA-r10作为一组CA的识别信息、在筛选出同组ca-BandwidthClassUL-r10、ca-BandwidthClassDL-r10、supportedMIMO-CapabilityDL-r10的信息,添加保存到字符串中,然后再把字符串作为元素添加到list中去。最后遍历list的元素写入目标文件 需求二: 然后收到反馈CA组合的理解是错误的。一组CA组合应该是以大括号作为识别的,里面可能包含多组: bandEUTRA-r10: ca-BandwidthClassUL-r10 ca-BandwidthClassDL-r10 supportedMIMO-CapabilityDL-r10 CA组合识别原理:在查看UECapabilityInformation内的CA组合后 发现CA组合内第一个 bandEUTRA-r10因为比其他bandEUTRA-r10多了一层的CA组合的大括号,所以如果给每一行增加索引的话就会发现除了第一个bandEUTRA-r10,其他bandEUTRA-r10到上一个supportedMIMO-CapabilityDL-r10的距离都是一样的,为了减少复杂度,我删除了所有’{’,这样所有除了所有CA组合第一个bandEUTRA-r10往上第四行是’}’其他bandEUTRA-r10的往上第四行都是supportedMIMO-CapabilityDL-r10 功能实现: 添加索引:便利时用了for enumerate()循环,这样便利时可以在循环时,自动为每个元素生成索引 CA组合识别:在识别到bandEUTRA-r10时,增加一个判断if datalist1[index-4].startswith(),如果bandEUTRA-r10的往上第四行是supportedMIMO-CapabilityDL-r10说明同组CA未结束,把筛选的有效信息强制类型转换后添加在上个元素末尾,反之则说明是个新的CA组合,往列表里添加一个新的元素。 需求三: 之后收到反馈CA组合虽然识别了,但是排序不行,需要按照CA组合支持的band进行排序 功能实现: 排序:于是我在识别完CA组合后,增加了一个循环和count(),用CA组合里的’-’给它们归类 比如1AA,11A,21AA是一类;1A-1A,2A-1AA,3A-1A是一类 在用一个中间变量保存开头的band的数字,一个类中把开头支持band的数字字母相同的CA组合归为一行 比如1A-21A,1A-22A一类1AA-2AA 1AA-3AA为一类 需求四: 之后收到反馈,CA组合分类不能只按照开头比较分类,不然一但数据多了会对查阅带来极大不便,应该按照每组CA组合中bandEUTRA-r10的值进行判断,比如1AA-2AA,1A-2AA和1AA-2A应该归在同一行 实现原理:首先我想的是按位比较数字,但是因为字母的数量不稳定,数字的位置不一定对应,然后我就想把数字全部提取出来作为索引,在相应的索引后面添加同组元素,用dict来实现排序。难点就在于从字符串中提取数字。后来在python的正则表达式中找到相关的处理函数compile()(设置匹配对象类型)和findall()(找到所有匹配对象并以list返回)。 功能实现: 第二次排序:在上次的排序中我保留了分类和从小到大的排序。方便提取索引时,索引也是从小到大。每遍历一个元素(CA组合有效信息),就compile()和findall(),从该元素中提取数字组合(在compile()的参数中添加()就能够使提取的内容成为一组数据),然后通过dict自带函数setdefault()添加索引,并可以设置索引值为list类型(dict类型的索引的值不可变,但如果类型为list,list的内容可以进行改动),避免重复索引,在本次遍历中完成将元素添加到索引值对应的list中去 需求五: 之后对程序进行测试,在测试test2时发现layers增加了fourlayers类型后,用来代表layers的数字2和4会影响分类结果。比如1AA(2)-1AA(2)和1A(4)-1A(2)会被归为两类。 test1:当CA组合的格式为xx-xx-xx-xx-xx(最长可识别为五位元素的组合,再长就需要修改代码) test2:当CA组合包含fourLayers test3:当CA组合缺失某种格式比如xx-xx时发现layers增加了fourlayers 功能实现: : 解除layers对排序的影响:用II 和 IV替代2,4来表示layers,测试后不影响阅读与分类 二、整体程序架构: 1.通过循环和自带的startswith()先将每组CA组合的有效信息识别 2.通过sorted()函数将所有CA组合从小到大排列 3.通过count()函数将所有CA组合根据格式不同分类 4.通过循环和正则表达式的split()对所有CAlist数据进行处理(用split处理只是防止出现不必要的错误) 5.通过循环和正则表达式compile()和findall()识别所有CA组合中数字,并将同一组合中的数字合为一个元素(在同一循环,用这个数字的元素作为一个dict的索引),用dict自带的setdefault()进行Key的添加顺便设置Key的值为list,避免Key重复,在用append把当前Key的字符串,添加到Key对应值的list中去 6.最后对dict整体遍历,将每一个Key的值输出到文本中去。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值