如何使用命令行参数分析外部数据

How to analyze external data using command line arguments

我开始编写分析外部数据的程序。这是在命令行参数的帮助下完成的。但是,我无法执行该程序..是我错了还是我在正确的轨道上...?

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("statistic", choices=["avg", "max"], help="Which statistic should be run?")
parser.add_argument("variable", choices=["distance", "delay"], help="What variable should be used for the calculation?")
parser.add_argument("tsvfile", help="Name of data file to be analyzed")
import pandas as pd
df = pd.read_csv("flights.tsv", sep="\t")

args = parser.parse_args()
s = args.statistic
v = args.variable
t = args.tsvfile

if s == "avg" and v == "distance" and t == "flights.tsv":
    print(df["DISTANCE"].mean())
elif s == "avg" and v == "delay" and t == "flights.tsv":
    print(df["DEPATURE_DELAY"].mean())
elif s == "max" and v == "delay":
    print(df["DEPATURE_DELAY"].max())
elif s == "max" and v == "distance" and t == "flights.tsv":
    print(df["DISTANCE"].max())

This is the exception I got

我真的很想得到一些帮助

如果您运行从命令行运行脚本,则需要添加参数(例如,python3 tom_script.py avg delay flights.tsv):

$ python3 tom_script.py  # Incorrect

usage: tom_script.py [-h] {avg,max} {distance,delay} tsvfile
tom_script.py: error: the following arguments are required: statistic, variable, tsvfile

$ python3 tom_script.py avg delay flights.tsv  # Correct

但是,对于 运行 Jupyter Notebook 中的代码,您必须为单元格内的参数提供值(例如,args = parser.parse_args(args=['avg', 'delay', 'flights.tsv', ]):

NOTE - Tested using Ubuntu 20.04, Python 3.8, IPython 7.13, Firefox 95.0, and Jupyter 6.0

in [1]: import argparse

in [2]: parser = argparse.ArgumentParser()

in [3]: parser.add_argument("statistic", choices=["avg", "max", ], help="Which statistic should be run?")
        parser.add_argument("variable", choices=["distance", "delay", ],
                            help="What variable should be used for the calculation?")
        parser.add_argument("tsvfile", help="Name of data file to be analyzed")

out[3]: _StoreAction(option_strings=[], dest='tsvfile', nargs=None, const=None, default=None, type=None, choices=None, help='Name of data file to be analyzed', metavar=None)

in [4]: # This does not work
        args = parser.parse_args()

        usage: ipykernel_launcher.py [-h] {avg,max} {distance,delay} tsvfile
        ipykernel_launcher.py: error: argument statistic: invalid choice: '/home/stack/.local/share/jupyter/runtime/kernel-66ce9d80-ab79-4f2f-9836-c98cdbcd20c5.json' (choose from 'avg', 'max')
        ERROR:root:Internal Python error in the inspect module.
        Below is the traceback from this internal error.
        ...

in [5]: # This does not work either (this also caused your exception)
        args = parser.parse_args(args=[])

        usage: ipykernel_launcher.py [-h] {avg,max} {distance,delay} tsvfile
        ipykernel_launcher.py: error: the following arguments are required: statistic, variable, tsvfile
        An exception has occurred, use %tb to see the full traceback.

        SystemExit: 2

in [6]: # This works
        args = parser.parse_args(args=['avg', 'delay', 'flights.tsv', ])

in [7]: s = args.statistic
        v = args.variable
        t = args.tsvfile

in [8]: print(s, v, t)

        avg delay flights.tsv