python - file as an optional parameter -


I'm new to Python and I'm developing a script that processes the file to calculate something. The script (preprocess.py) in the command line is called like this:

  dragon preprocess.py if.txt of.txt 13 13 41 41 52 24  

if.txt is the input file, of.txt is the output file, (13 13 41 41 52 24) is just the size of the groups and the subgroup of data.

In a part of the script, a calculation is done with two variables (mean = 0 and STD = 1).

I have to modify the script so that the script can accept an optional parameter, a file containing 2 values ​​(mean and STD), therefore, if the script is called as above, calculate = 0 and Std = 1, but if the script is called like this:

  python preprocess.py if .txt of.txt 13 13 41 41 52 24 xxxx.txt < / Code> 

The script takes two values ​​in the xxxx.txt file and each value is associated with its respective variables (mean and STD ).

The script now gets the parameters with the sys.argv [] command. I am looking for ways to solve it and found that the argparse module can solve it, but I do not understand how to get value from the file and save it from the desired.

  1. How to use the xxxx.txt file to create an optional parameter in the argparse module and how to save two values ​​in that file in two variables (mean and std)?

  2. If I use the Argoners module, then argparse must be used to get all the other parameters. If so, how to do it?

: PD: I am learning English, so if I made a mistake, please correct me.

If the number of int value is fixed, then this script will work

 

the'? ' Means that the final argument is optional.

A variable number of intits can be taken with

  parser.add_argument ('wars', type = int, nargs = '+'.) < / Code> 

However, 2 variable nargs do not work together. '+' Everything will be found, '' except for no one left.

I think the alternative file should be suggested to create an 'alternative / flag' argument. Generally, alternate people are handled, and the handling of the situation gets around this ambiguity.

  parser.add_argument ('- file3') Python preprocess.py if.txt of.txt 13 13 41 41 52 24 - File 3 xxxx.txt  

Check for a non argparse approach last sys.argv value. If it does not look like a int , then assume that this is the name of the file, and proceed with parsing as before.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -