KeyError: None + argparse python 3.xx

KeyError: None + argparse python 3.xx

main():
parser = argparse.ArgumentParser(description="IVS code")
parser.add_argument("--prod", help="Product to import config for")
parser.add_argument("--wf", help="File to write results to")
parser.add_argument("--ipf", help="IVS product family file") #not in use yet, might keep all ivs product family in same file
args=parser.parse_args()
print (args)

dataDF,keepDF,rulesDF,family_list,level_list,price_override_dict = getInputDataStructures(args)

============================================= ================================

函数

def getInputDataStructures(args):

#Create the price override dictionary
    reader = csv.reader(open(cfg[args.prod]["po"], 'r'), skipinitialspace=True)
   #reader = csv.reader(open(cfg[args.prod]["po"], 'r'), skipinitialspace=True)
    next(reader, None)  # skip the headers
    price_override_dict = {}
    for row in reader:
        cd,cntry,price = row
        price_override_dict[cd+'|'+cntry] = price

============================================= ========================== YAML 文件 -> 套装和杂项:

lvl: "ITEM_CODE,REGIONS,CLUSTERS,SUBCLUSTER,COUNTRY"
kl: Keep.csv
rs: rules.csv
pf: "Sets and Misc"
ps: "-"
pg: Product_Family.csv 
dt: "Sets_Misc.rpt"
po: "priceOverride.csv"

============================================= ================================ 错误输出:

24]: runfile('.../Workspace/CodeFiles/volume_substitution_v3.py', wdir='.../Workspace/CodeFiles')
Reloaded modules: config
Namespace(ipf=None, prod=None, wf=None)
Traceback (most recent call last):

  File "<ipython-input-24-6b4c2ae644d5>", line 1, in <module>
    runfile('.../Workspace/CodeFiles/volume_substitution_v3.py', wdir=.../CodeFiles')


  File ".../Workspace/CodeFiles/volume_substitution_v3.py", line 447, in <module>
    main()

  File ".../Workspace/CodeFiles/volume_substitution_v3.py", line 130, in main
    dataDF,keepDF,rulesDF,family_list,level_list,price_override_dict = getInputDataStructures(args)

  File ".../CodeFiles/volume_substitution_v3.py", line 21, in getInputDataStructures
    reader = csv.reader(open(cfg[args.prod]["po"], 'r'), skipinitialspace=True)

KeyError: None

我如何 运行 它或应该向 运行 这段代码提供什么输入? 有什么方法可以使此代码正常工作?

我在编辑器中创建了这个文件:

import argparse

def main():
    parser = argparse.ArgumentParser(description="IVS code")
    parser.add_argument("--prod", help="Product to import config for")
    parser.add_argument("--wf", help="File to write results to")
    parser.add_argument("--ipf", help="IVS product family file") #not in use yet, might keep all ivs product family in same file
    args=parser.parse_args()
    return args

if __name__ == '__main__':
    args=main()
    print (args)

在终端中 window (linux bash) 我做了一些样本运行:

不带参数调用脚本我得到默认值:

1449:~/mypy$ python stack51971647.py 
Namespace(ipf=None, prod=None, wf=None)

有了 -h 我得到了帮助:

1449:~/mypy$ python stack51971647.py -h
usage: stack51971647.py [-h] [--prod PROD] [--wf WF] [--ipf IPF]

IVS code

optional arguments:
  -h, --help   show this help message and exit
  --prod PROD  Product to import config for
  --wf WF      File to write results to
  --ipf IPF    IVS product family file

具有各种输入值:

1449:~/mypy$ python stack51971647.py --prod foobar
Namespace(ipf=None, prod='foobar', wf=None)

1450:~/mypy$ python stack51971647.py --prod foobar --wf afile.py --ipf xxx
Namespace(ipf='xxx', prod='foobar', wf='afile.py')

来自具有相同工作目录的 Ipython 会话:

In [156]: run stack51971647
Namespace(ipf=None, prod=None, wf=None)
In [157]: run stack51971647 --prod foobar
Namespace(ipf=None, prod='foobar', wf=None)

我没有 spyder 或 pycharm,所以无法提供这些变体。