2016-04-04 11 views
0

argparseライブラリを使用して、スクリプト内の異なる引数を使用しています。私はresult.txtファイルに以下の結果の出力を渡しています。argparse出力から引数のパラメータを抽出する

テーブル名はtest_argumentsです。ここでは、異なる引数名と説明を格納する必要があります。

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) ; 

を私はこのファイルを読み込み、以下の「result.txt」ファイルからこれらの二つのフィールドを抽出することができますどのように:私の下からの例では、挿入する必要がありますか?それを行う最良の方法はどれですか?

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 

更新:私のコード

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) 

出力:

num   The fibnocacci number to calculate: 

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

を、私はこのヘルプメッセージを表示し、(ファイル、テキストファイルへの出力)と(助けを抽出しようとしているわけではありませんこのリストからは抜け出すことができますが、解析することは困難です。これに関するすべての入力?

+0

など、「--file -f」から「ファイル」を抽出する必要があり36380688/extract-values-from-parse-add-argument-in-argparse-python?その中で、あなたは 'parser'オブジェクトにアクセスできました。ここであなたは 'help'メッセージにのみアクセスできますか? – hpaulj

+0

@hpaulj私は実際にそのスクリプトのコードを変更したくありません。私は、この出力ファイルを読み込んで引数データを解析する新しいスクリプトを書くつもりでした。 –

+0

私はあなた自身のためにテキストを解析する必要があると思います。 – hpaulj

答えて

1

はここで2つのスペースがhelpラインを区別するよう

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() 

が見えるあなたのヘルプテキストの構文解析でのスタートです。 formatterコードを確認する必要がありますが、helpのテキストを並べて、明確に区別する試みがあると思います。 2つの以上のスペースに基づいて

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'] 

分割線ストリングsplit方法よりre.splitと容易です。実際にはreを使用してarglinesを収集した可能性があります。私は引数のグループ名(positional argumentsなど)をチェックすることもできます。

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']] 

今、私はちょうどこれがあなたの前の質問、http://stackoverflow.com/questions/異なる方法

+0

私は自分のコードで質問を編集し、今まで出力しています。私は値のリストを得ましたが、それらの2つのフィールドを抽出するために解析する必要があります。 –

+0

あなたの行は '' + ''で分割することができます - 2つ以上のスペース。また、 'optionals'は' positionals'のヘルプに合わせるべきです。だから、あなたは 'ヘルプ'インデントを識別するために使用することができます。 – hpaulj

関連する問題