Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #63524 > unrolled thread
| Started by | sagarnildass@gmail.com |
|---|---|
| First post | 2014-01-08 13:51 -0800 |
| Last post | 2014-01-08 18:57 -0500 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
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
| From | sagarnildass@gmail.com |
|---|---|
| Date | 2014-01-08 13:51 -0800 |
| Subject | python copy selected lines from one file to another using argparse or getopt |
| Message-ID | <bc99af4e-8031-477c-877f-a5460f8a09ab@googlegroups.com> |
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?
Here's my code as of now:
#/Python33
import sys
import os
def main(): #main method
try:
f1 = open(sys.argv[1], 'r') #takes the first input file in command line
found = False
user_input1 = (sys.argv[3]) #takes the word which is to be excluded.
user_input2 = (sys.argv[4]) #takes the word which is to be included.
if sys.argv[1] == sys.argv[2]:
f1.close()
sys.exit('\nERROR!!\nThe two file names cannot be the same.')
if sys.argv[3] != sys.argv[4]:
for line in f1:
if user_input1 in line or user_input2 in line:
f2 = open(sys.argv[2], 'a')
if user_input1 in line:
if user_input2 in line:
pass
elif user_input2 in line:
f2.write(line)
found = True
f2.close()
if not found:
print("ERROR: The Word couldn't be found.")
f1.close()
if sys.argv[3] == sys.argv[4]:
f1.close()
sys.exit('\nERROR!!\nThe word to be excluded and the word to be included cannot be the same.')
except IOError:
print('\nIO error or wrong file name.')
except IndexError:
print('\nYou must enter 5 parameters.') #prevents less than 5 inputs which is mandatory
except SystemExit as e: #Exception handles sys.exit()
sys.exit(e)
if __name__ == '__main__':
main()
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-01-08 22:53 +0000 |
| Message-ID | <lakkt4$1hj$1@reader1.panix.com> |
| In reply to | #63524 |
In <bc99af4e-8031-477c-877f-a5460f8a09ab@googlegroups.com> sagarnildass@gmail.com writes:
> 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.
This should help you get started:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-search', '-s', help='a word to search',
action='append', required=True)
parser.add_argument('-exclude', '-e', help='a word to exclude from search',
action='append')
parser.add_argument('input_file')
parser.add_argument('output_file')
args = parser.parse_args()
Assuming the user provided correct arguments, the args object will have
these attributes:
args.search - a list of words to search for.
args.exclude - a list of words to exclude. Can be None.
args.input_file - the input file name.
args.output_file - the output file name.
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-01-08 23:03 +0000 |
| Message-ID | <mailman.5205.1389222258.18130.python-list@python.org> |
| In reply to | #63524 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-01-08 18:57 -0500 |
| Subject | Re: python copy selected lines from one file to another using argparse or getopt |
| Message-ID | <mailman.5212.1389225340.18130.python-list@python.org> |
| In reply to | #63524 |
On Wed, 8 Jan 2014 13:51:40 -0800 (PST), 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.
John Gordon has given you a good start on argument parsing. So let's
start with some general comments.
Please indent by 4 columns. Please factor your code into multiple
functions so it's readable, especially by you. Please learn that
complementary conditions are best handled by else not by another if.
So the arguments to your main function should probably be two file
objects and two lists. The function should call another one for each
list, in a loop that looks something like
for line in infile:
if check_include (includes, line and not check_exclude (excludes,
line):
dosomethingwith line
By the way checking the 2 filenames for equals doesn't begin to
protect against trashing some file. Better to check if the output
file exists, perhaps by opening in append mode and checking size.
--
DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web