Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63529

Re: python copy selected lines from one file to another using argparse or getopt

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: python copy selected lines from one file to another using argparse or getopt
Date 2014-01-08 23:03 +0000
References <bc99af4e-8031-477c-877f-a5460f8a09ab@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5205.1389222258.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 08/01/2014 21:51, sagarnildass@gmail.com wrote:
> I am trying to write a program in python which searches for user specified words in a txt file and copies the selected lines containing that word into another file.
>
> Also the user will have an option to exclude any word.
>
> (e.g Suppose the user searches for the word "exception" and want to exclude the word "abc", then the code will only copy the lines which has "exception" in it but not "abc").
>
> Now all the work will be done from the command prompt.
>
> The input would be:
>
> file.py test.txt(input file) test_mod.txt(output file) -e abc(exclude word denoted by -e)-s exception(search word denoted by -s)
> Now the user will have an option to enter multiple exclude words and multiple search words.
>
> Now so far I have achieved that the input format is:
>
> file.py test.txt test_mod.txt abc exception".
> This excludes the word "abc" and search for "exception".
>
> But I don't know how to:
>
> Include multiple search word and exclude words
> How to denote them by -e and -s. I have seen the argparse and the getopt tutorial. But there's no tutorial on this specific topic.
> Please can somebody help me by modifying my code or write a new one?
>

If you can use third party modules I suggest you look at docopt, it's 
available on pypi.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

python copy selected lines from one file to another using argparse or getopt sagarnildass@gmail.com - 2014-01-08 13:51 -0800
  Re: python copy selected lines from one file to another using argparse or getopt John Gordon <gordon@panix.com> - 2014-01-08 22:53 +0000
  Re: python copy selected lines from one file to another using argparse or getopt Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-08 23:03 +0000
  Re: python copy selected lines from one file to another using argparse or
 getopt Dave Angel <davea@davea.name> - 2014-01-08 18:57 -0500

csiph-web