[英]How to convert a string to a variable / list name?
I have a list of stock indexes (indizies) and several lists of stock tickers (eg gdaxi, mdaxi).我有一个股票指数列表(indizies)和几个股票代码列表(例如gdaxi、mdaxi)。
I want to download the stocks from yahoo in two loops.我想分两个循环从雅虎下载股票。
Background: In the real program the user can choose which index, indexes he wants to download.背景:在实际程序中用户可以选择自己要下载的索引,索引。
The problem is, that the type of index_name is a string and for the second loop index_name has to be a list.问题是,index_name 的类型是一个字符串,而对于第二个循环,index_name 必须是一个列表。 But the second loop takes index_name as a string.
但是第二个循环将 index_name 作为字符串。
Result:It trys to download the csv for g,d,a,x,i
结果:它尝试为
g,d,a,x,i
下载 csv
Question: How can I transform index_name from string to list?问题:如何将 index_name 从字符串转换为列表?
from pandas_datareader import data as pdr
indizies = ['GDAXI', 'MDAXI']
gdaxi = ["ADS.DE", "AIR.DE", "ALV.DE"]
mdaxi = ["AIXA.DE", "AT1.DE"]
for index_name in indizies:
for ticker in index_name:
df = pdr.get_data_yahoo(ticker)
df.to_csv(f'{ticker}.csv')
In ticker in index_name
you are iterating over the letters in given strings.在
ticker in index_name
您正在迭代给定字符串中的字母。
I guess you to change your code to something like:我猜您将代码更改为:
from pandas_datareader import data as pdr
gdaxi = ["ADS.DE", "AIR.DE", "ALV.DE"]
mdaxi = ["AIXA.DE", "AT1.DE"]
indizies = [gdaxi, mdaxi]
for index_name in indizies:
for ticker in index_name:
df = pdr.get_data_yahoo(ticker)
df.to_csv(f'{ticker}.csv')```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.