KeyError:无+ argparse python 3.xx

时间:2018-08-22 16:49:36

标签: python yaml argparse

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

如何运行它或应提供什么输入来运行这段代码? 使该代码正常工作的方法是什么?

1 个答案:

答案 0 :(得分:0)

在编辑器中,我创建此文件:

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)

在终端窗口(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,所以对这些变化没有帮助。