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']
を、私はこのヘルプメッセージを表示し、(ファイル、テキストファイルへの出力)と(助けを抽出しようとしているわけではありませんこのリストからは抜け出すことができますが、解析することは困難です。これに関するすべての入力?
など、「--file -f」から「ファイル」を抽出する必要があり36380688/extract-values-from-parse-add-argument-in-argparse-python?その中で、あなたは 'parser'オブジェクトにアクセスできました。ここであなたは 'help'メッセージにのみアクセスできますか? – hpaulj
@hpaulj私は実際にそのスクリプトのコードを変更したくありません。私は、この出力ファイルを読み込んで引数データを解析する新しいスクリプトを書くつもりでした。 –
私はあなた自身のためにテキストを解析する必要があると思います。 – hpaulj