循环中的动态变量python

2024-05-19 00:43:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这里浏览了很多帖子,想找到一个解决方案,但也许我找错了短语。我是python新手,我的代码问题如下:

from yahoo_finance import Share
for line in open('fcts.txt'):
   yahoo = Share('YHOO')
   print (yahoo +'.' + line)

对于fcts.txt内的每个函数,它基本上应该执行以下操作:

^{pr2}$

fcts.txt包含不同的函数,如

get_open()
get_change()
...

谢谢你, 斯特凡


Tags: 函数代码fromimporttxtsharegetline
1条回答
网友
1楼 · 发布于 2024-05-19 00:43:20

您可以按以下名称访问方法:

from yahoo_finance import Share

with open('fcts.txt') as methodsFile:
  for methodName in methodsFile:
    yahoo = Share('YHOO')
    method = getattr(yahoo, methodName.strip())
    print (method())

但在我看来这很粗糙。在

相关问题 更多 >

    热门问题