optparse和字符串 [英] optparse and strings

查看:102
本文介绍了optparse和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试学习如何使用outparse.因此,在这种情况下,我认为我的设置正确无误,只是如何设置选项有点令人困惑.基本上,我只想检查我的文件名以查看是否有特定的字符串.

Trying to learn how to use outparse. So here is the situation, I think I got my setup correct its just how to set my options is kinda... confusing me. Basically I just want to check my filename to see if there are specific strings.

例如:

    python script.py -f filename.txt -a hello simple

我希望它返回类似...的信息

I want it to return something like...

    Reading filename.txt....
    The word, Hello, was found at: Line 10
    The word, simple, was found at: Line 15

这是我到目前为止的内容,我只是不知道如何正确设置.很抱歉提出愚蠢的问题:P.预先感谢.

Here is what I have so far, I just don't know how to set it up correctly. Sorry for asking silly questions :P. Thanks in advance.

这是到目前为止的代码:

Here is the code thus far:

    from optparse import OptionParser

    def main():

        usage = "useage: %prog [options] arg1 arg2"
        parser = OptionParser(usage)

        parser.add_option_group("-a", "--all", action="store", type="string", dest="search_and", help="find ALL lines in the file for the word1 AND word2")

        (options, args) = parser.parse_args()

        if len(args) != 1:
            parser.error("not enough number of arguments")

            #Not sure how to set the options...


    if __name__ == "__main__":
        main()

推荐答案

您应该使用OptionParser.add_option() ... add_option_group()并没有按照您的想法做...这是一个完整的例子,您需要执行的操作...请注意,--all依赖于逗号分隔值...这使它变得更容易,而不是使用空格分隔(这需要引用--all的选项值.

You should use OptionParser.add_option()... add_option_group() isn't doing what you think it does... this is a complete example in the spirit of what you're after... note that --all relies on comma-separating the values... this makes it easier, instead of using space separation (which would require quoting the option values for --all.

还请注意,您应该显式检查options.search_andoptions.filename,而不是检查args

Also note that you should check options.search_and and options.filename explicitly, instead of checking the length of args

from optparse import OptionParser

def main():
    usage = "useage: %prog [options]"
    parser = OptionParser(usage)
    parser.add_option("-a", "--all", type="string", dest="search_and", help="find ALL lines in the file for the word1 AND word2")
    parser.add_option("-f", "--file", type="string", dest="filename", help="Name of file")
    (options, args) = parser.parse_args()

    if (options.search_and is None) or (options.filename is None):
        parser.error("not enough number of arguments")

    words = options.search_and.split(',')
    lines = open(options.filename).readlines()
    for idx, line in enumerate(lines):
        for word in words:
            if word.lower() in line.lower():
                print "The word, %s, was found at: Line %s" % (word, idx + 1)

if __name__ == "__main__":
    main()

使用相同的示例,使用... python script.py -f filename.txt -a hello,simple

Using your same example, invoke the script with... python script.py -f filename.txt -a hello,simple

这篇关于optparse和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆