CommandLine Option in Python for filtering values from Column

balaji marisetti balajimarisetti at gmail.com
Thu Apr 10 01:16:47 EDT 2014


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 <rohan_1925 at yahoo.co.in> 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



More information about the Python-list mailing list