Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; 'args': 0.07; 'parser': 0.07; 'skip:` 10': 0.07; 'sys': 0.07; 'arguments': 0.09; 'delimited': 0.09; 'parameter': 0.09; 'rows': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; '#parse': 0.16; 'arg': 0.16; 'be:': 0.16; 'line)': 0.16; 'subject:values': 0.16; 'tab': 0.16; 'wrote:': 0.18; 'module': 0.19; 'skip:p 40': 0.19; 'entered': 0.20; 'command': 0.22; 'example': 0.22; 'import': 0.22; 'python?': 0.22; 'cc:addr:python.org': 0.22; 'parse': 0.24; 'script.': 0.24; 'skip:` 20': 0.24; 'cc:2**0': 0.24; 'options': 0.25; 'script': 0.25; 'this:': 0.26; 'values': 0.27; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'file': 0.32; 'option': 0.32; 'url:python': 0.33; 'running': 0.33; 'subject:from': 0.34; 'display': 0.35; 'received:google.com': 0.35; 'accessing': 0.36; 'url:listinfo': 0.36; 'url:org': 0.36; 'list.': 0.37; 'filter': 0.38; 'mapping': 0.38; 'url:library': 0.38; '(from': 0.39; 'itself': 0.39; 'skip:p 20': 0.39; 'url:mail': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; '5th': 0.60; 'skip:a 30': 0.61; 'field': 0.63; 'more': 0.64; 'url:pdf': 0.68; 'limit': 0.70; 'quality': 0.72; 'saw': 0.77; 'column.': 0.84; 'type=int,': 0.84; 'to:addr:yahoo.co.in': 0.91; 'url:latest': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=lwV0/aVXuqEcgJDy9ErHI6QT498fky6kraKaXbIfJHc=; b=sufSx4fk5f61IxWnwLJWIRcR6p0YZInbxWfv9iRsKZGEXKytPGwyqu0ym5MJFabkEM WZ1zSmjRE5JCanj2E1L18ijEX9A8N5pdLDW3QCST4M0gg2rcBKkbt+JJq5YusC1Gbr/b mVIL8/ZXyS26gMzglzgtpU/S9j4J+HIYUlqfrEXHsYC1LaU9Vf0PrSr+x3caqnOC87n2 foBow838HlGppxPVrZ7kXUfqznEWa+jXiOHiRmXFoP4FtiaE7JfQ3v+hPjMI/zTUgRPm mcPqHjTLfkIi0UiZqKpYoqJg4xGxjaLiEDPvW75ETCoJ9TDmO6Qv9O7gYSIR2/NsCk6E lWYA== X-Received: by 10.183.3.102 with SMTP id bv6mr12415662obd.18.1397107038642; Wed, 09 Apr 2014 22:17:18 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <1397071383.35192.YahooMailNeo@web193506.mail.sg3.yahoo.com> References: <1397071383.35192.YahooMailNeo@web193506.mail.sg3.yahoo.com> From: balaji marisetti Date: Thu, 10 Apr 2014 10:46:47 +0530 Subject: Re: CommandLine Option in Python for filtering values from Column To: rohan bareja Content-Type: text/plain; charset=UTF-8 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 84 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397107041 news.xs4all.nl 2948 [2001:888:2000:d::a6]:46107 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70011 sys.argv is itself a list. You can directly access the `quality` field as sys.argv[2]. quality = int(sys.argv[2]) However, if you want to use more command line options, then using `argparse` module is better than accessing arguments using `sys.argv`. Import `argparse` module and create a parser import argparse parser = argparse.ArgumentParser(description='My example command line option parser') #Add as many arguments as you want to parse (from the command line) parser.add_argument('--quality', type=int, help='Quality score') parser.add_argument("--some-other-option", help="Some other option for example") ... ... #Parse the arguments (to be)given on the command line(when the script is invoked) args = parser.parse_args() #Access all the arguments or options as `args.quality`, `args.some_other_option`, etc. print("quality =", args.quality) print("some-other-option =", args.some_other_option) Read the documentation of argparse for better understanding. https://docs.python.org/2/library/argparse.html On 10 April 2014 00:53, rohan bareja wrote: > I want to write a function in Python for a tab delimited file I am dealing > with,to filter out values from a column, and put that parameter as a command > line option while running the script. > > So,if the limit is 10,the filter out all the rows with values less than 10. > Also,I want to check if the number entered is numeric or not else display > the message "its not numeric". > > > So,the command line would be: > > python script.py file --quality [limit] > > > The Python script: > > import sys > arg = [] > for a in sys.argv: > arg.append(a) > quality = arg[2] > > > To be more specific,the file I am dealing with is a Samfile,and using > package > Pysam,which has mapping quality limits in the 5th column. > > https://media.readthedocs.org/pdf/pysam/latest/pysam.pdf > > Commandline: > > python script.py samfile --quality [limit] > > I am reading the samfile using this: > > samfile = pysam.Samfile(arg[1], "rb" ) > mapqlim = arg[2] > > I am a Python beginner,but saw one of the modules,argparse. How can I > accomplish this using argparse in Python? > > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- :-)balaji