我对python很新,需要一些指导。我试图从控制台传递一些变量并获取错误消息:
AuctionStrategy_2.0.py: error: argument -s/--sectorStocks: invalid int value: 'tep3'
当我运行控制台命令时:
run AuctionStrategy_2.0.py -in10 -out5 -rolls15 -step3 -t.001 -s5 -m100 -v50 -e'01/01/2016'
有人能告诉我如何解决这个问题吗?我的代码除了尝试从控制台传递变量之外什么都不做。请参阅下面的代码:
import argparse
import os
import fnmatch
import pandas as pd
from pandas.tseries.offsets import BDay
import lzma
import numpy as np
import math
import datetime
def main():
print('Processing args....')
insampleLength,outsampleLength,rolls,step,threshold,minStocksPerSector,minMarketCap,minVolume,endDate = get_args()
print(insampleLength,outsampleLength,rolls,step,threshold,minStocksPerSector,minMarketCap,minVolume,endDate)
rawDataPath = 'C:/Users/simon/Documents/data/close_unadjusted/close_unadjusted/'
def get_args():
'''This function parses and return arguments passed in'''
insampleLength = 0
outsampleLength = 0
rolls = 0
step = 0
endDate =''
minStocksPerSector = 0
threshold = 0
parser = argparse.ArgumentParser(
description='Args to run VWAP Auction simulation')
''' Command line arguments'''
parser.add_argument('-in', '--inSampleDataLength', type=int, help='Number of historic epochs insample', required=True)
parser.add_argument('-out', '--outSampleDataLength', type=int, help='Number of historic epochs outsample', required=True)
parser.add_argument('-rolls', '--numberRolls', type=int, help='Number of rolls', required=True)
parser.add_argument('-step', '--rollStep', type=int, help='Number of historic epochs', required=True)
parser.add_argument('-t','--threshold', type=float, help='starting value', required=True)
parser.add_argument('-s','--sectorStocks', type=int, help='minimum number', required=True)
parser.add_argument('-m','--marketCapCutOff', type=int,help='market capitalisation', required=True)
parser.add_argument('-v','--volumeCutOff', type=int, help='daily volume', required = True)
parser.add_argument('-e', '--endDate', type=str,help='last day of testing',required = True)
args = parser.parse_args()
''' Assign args to variables'''
insampleLength = args.inSampleDataLength
outsampleLength = args.outSampleDataLength
rolls = args.numberRolls
step = args.rollStep
threshold = args.threshold
minStocksPerSector = args.sectorStocks
minMarketCap = args.marketCapCutOff
minVolume = args.volumeCutOff
endDate = datetime.datetime.strptime(args.endDate, "%d-%b-%Y")
return insampleLength,outsampleLength,rolls,step,threshold,minStocksPerSector,minMarketCap,minVolume,endDate
if __name__ == "__main__":
print ("AuctionStategy_1.0...25/03/16")
try:
main()
except KeyboardInterrupt:
print ("Ctrl+C pressed. Stopping...")
答案 0 :(得分:1)
单个破折号始终标识单个字符参数。但是你试图定义def main():
infile = open("WTExcerpt.xml", "r", encoding="utf8")
outfile = open("DemoWT.html", "w")
print("<html>\n<head>\n<title>Winter's Tale</title>\n",file=outfile)
print("<link rel='stylesheet' type='text/css' href='Shakespeare.css'>\n</head>\n<body>\n",file=outfile)
for line in infile:
text = line.replace("<w>", "")
if "<title>" in text and "</title>" in text:
print("<h1>",text,"</h1>\n",file=outfile)
elif text=="<head>":
in_headline = True
headline = ""
elif text == "</head>":
in_headline = False
print("<h3>", headline, "</h3>\n", file=outfile)
elif in_headline:
headline += text
main()
;这被解释为-step
,稍后由实际的-s
参数重新定义。
您应该为“步骤”选择不同的标识符,或者始终使用双破折号版本-s
。
答案 1 :(得分:0)
参数-s
需要一个整数,你给了一个字符串,这会导致你得到的错误。
run AuctionStrategy_2.0.py -in 10 -out 5 -rolls 15 -step 3 -t .001 -s 5 -m 100 -v 50 -e '01/01/2016'
希望这有帮助