【发布时间】:2018-06-20 08:17:39
【问题描述】:
所以我编写了一个运行良好的程序。它根据股票代码从网站中提取股票数据,然后根据我想要的行和列将其放入 Excel 电子表格中。我希望能够对多只股票进行此操作,但是我不知道如何编写函数。所以,我一直在做的是一遍又一遍地复制和粘贴代码。我的代码中唯一改变的参数是代码和标记为 r、x 和 y 的行号。有人可以帮我为我的程序编写一个函数吗?
fd = gm.FinancialsDownloader()
fd_frames = fd.download('AAPL')
wb = UpdateWorkbook(r'C:\Users\vince\Project\Stock Python Project\Spreadsheet.xlsx', worksheet=1)
i_s = fd_frames['income_statement']
i_s.set_index('title', inplace=True)
i_s = i_s.drop('parent_index', axis=1)
i_s = i_s.loc[['Revenue','Operating expenses']] #Add all the names you want from income statement
i_s = i_s/(10**9)
b_s = fd_frames['balance_sheet']
b_s.set_index('title', inplace=True)
b_s = b_s.drop('parent_index', axis=1)
b_s = b_s.loc[['Assets','Current assets']]
b_s = b_s/(10**9)
c_f = fd_frames['cash_flow']
c_f.set_index('title', inplace=True)
c_f = c_f.drop('parent_index', axis=1)
c_f = c_f.loc[['Free Cash Flow','Operating cash flow']]
c_f = c_f/(10**9)
r = 6 # starts at whatever row
c = 13 # column 'M'
for l in i_s.values.tolist():
for item in l:
wb.ws.cell(row=r, column=c).value = item
c += 1 # Column 'N'
c = 13
r += 1
x = 21
for l in b_s.values.tolist():
for item in l:
wb.ws.cell(row=x, column=c).value = item
c += 1 # Column 'N'
c = 13
x += 1
y = 41
for l in c_f.values.tolist():
for item in l:
wb.ws.cell(row=y, column=c).value = item
c += 1 # Column 'N'
c = 13
y += 1
wb.save()
【问题讨论】:
标签: excel function user-defined-functions