【发布时间】:2020-12-22 14:59:51
【问题描述】:
我似乎无法在 Python 中使用这种格式。我正在尝试定义一个包含表单参数的函数 - “[Some].[Name]”
谁能告诉我我是怎么做到的?我想我已经尝试了 ' 和 " 的所有组合,但不管论点中的 [.] 和 ["] 似乎都不起作用。
在下面的代码中,我试图将参数定义为“VWS.co”
def get_stock_data(Company):
#This function defines the data to be collected.
#send a get request to query Company's end of day stock prices in period
global VWS_data
Stock_data = yf.Ticker(Company)
Stock_data = Stock_data.history(period="5y")
# look at the first 5 rows of the dataframe
print(Stock_data)
print(Stock_data.describe(include='all'))
get_stock_data("VWS.co")
编辑: 使用转义字符 get_stock_data(""VWS.co"") 使定义生效。但是,还是有些不对劲。当我运行脚本时,它仍然只能使用“VWS.co”作为定义。请参见下面的代码, VWS_data_with_arg 有效。 VWS_data 没有。我在这里遗漏了一些非常明显的东西吗?
def get_stock_data(Company):
Stock_data = yf.Ticker("VWS.co")
Stock_data_with_arg = yf.Ticker(Company)
VWS_data = Stock_data.history(period="5y")
VWS_data_with_arg = Stock_data_with_arg.history(period="5y")
print(VWS_data) #This returns the expected values
print(VWS_data_with_arg) #This returns an empty dataset
get_stock_data("\"VWS.co\"")
【问题讨论】:
-
get_stock_data('"VWS.co"')会帮你的!
标签: python function format arguments