0

I am using argparse library to use different arguments in my script. I am passing the output of the below result to the result.txt file.

I have a table name test_arguments where i need to store different argument name and description. example from below i need to insert :

 Insert into table test_argument ( arg_name, arg_desc ) as  ( num, The fibnocacci number to calculate:);
 Insert into table test_argument ( arg_name, arg_desc ) as  ( help, show this help message and exit)   ;
 Insert into table test_argument ( arg_name, arg_desc ) as  ( file, output to the text file)   ;

How can i read this file and extract these two fields from the below 'result.txt' file? Which is the best way to do it?

python sample.py -h >> result.txt

result.txt
-----------
usage: sample.py [-h] [-f] num

To the find the fibonacci number of the give number

positional arguments:
num         The fibnocacci number to calculate:

optional arguments:
-h, --help  show this help message and exit
-f, --file  Output to the text file

Updated : My code

import re
list = []
hand = open('result.txt','r+')
for line in hand:
line = line.rstrip()
if re.search('positional', line) :
    line = hand.readline()
    print(line)
elif re.search('--',line):
    list.append(line.strip())

print(list)

Output :

num         The fibnocacci number to calculate:

['-h, --help  show this help message and exit', '-f, --file  Output to the text file']

I am not trying to extract (file,Output to the text file) and (help,show this help message and exit) from this list but finding it difficult to parse them. Any inputs on this?

4
  • How is this different from your previous question, stackoverflow.com/questions/36380688/…? In that one you had access to the parser object. Here do you only have access to the help message? Commented Apr 4, 2016 at 20:19
  • @hpaulj I actually dont want to alter the code in that script. I was planning to write a new script that reads this output file and parses for argument data. Commented Apr 4, 2016 at 20:22
  • I think you will just have to parse the text for yourself. Commented Apr 4, 2016 at 20:43
  • @hpaulj Is there no way to read this file, use rstrip(), search() and extract these terms? I was trying something like that, but not able to extract it completely. Commented Apr 4, 2016 at 20:48

1 Answer 1

1

Here's a start at parsing your help text

In [62]: result="""usage: sample.py [-h] [-f] num

To the find the fibonacci number of the give number

positional arguments:
num         The fibnocacci number to calculate:

optional arguments:
-h, --help  show this help message and exit
-f, --file  Output to the text file"""

In [63]: result=result.splitlines()

Looks like 2 spaces distinguishes the help lines. I'd have to check the formatter code, but I think there is an attempt to line up the help texts, and clearly separate them.

In [64]: arglines=[line for line in result if '  ' in line]
In [65]: arglines
Out[65]: 
['num         The fibnocacci number to calculate:',
 '-h, --help  show this help message and exit',
 '-f, --file  Output to the text file']

Splitting lines based on 2 or more spaces is easier with re.split than the string split method. In fact I probably could have used re to collect arglines. I could also have checked for the argument group names (positional arguments etc).

In [66]: import re
In [67]: [re.split('  +',line) for line in arglines]
Out[67]: 
[['num', 'The fibnocacci number to calculate:'],
 ['-h, --help', 'show this help message and exit'],
 ['-f, --file', 'Output to the text file']]

Now I just have to extract 'file' from '-f, --file', etc.

Sign up to request clarification or add additional context in comments.

2 Comments

I have edited the question with my code and output till now. I got a list of values but need to parse it for extracting those two fields.
Your lines can still be split on ' +' - 2 or more spaces. Also the optionals helps should line up with the positionals help. So you could use the one identify the 'help' indent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.