想要在循环中使用列表来改变变量的值。

huangapple go评论100阅读模式
英文:

want to change the value of variable using list in loop python

问题

以下是翻译好的代码部分:

  1. symbols = ["NIFTY", "ITC", "BBC", ....etc]
  2. from tvDatafeed import TvDatafeed, Interval
  3. tv = TvDatafeed()
  4. tv = TvDatafeed(username, password, chromedriver_path=None)
  5. tv = TvDatafeed(auto_login=true)
  6. nifty_data = tv.get_hist(symbol='NIFTY', exchange='NSE', interval=Interval.in_1_hour, n_bars=1)
  7. print(nifty_index_data)
  8. #result will be like this in pandas>>>>>>>
  9. | datetime | symbol | open | high | low | close | volume |
  10. | --- | --- | --- | --- | --- | --- | --- |
  11. | 2023-07-20 08:45:00 | NSE:NIFTY | 19943.15 | 19991.85 | 19935.45 | 19987.05 | 60215122.0 |
  12. There is around 1060 stocks/symbols and we have to run script again-again to scrap different data for different symbols like this \>\>\>
  13. # nifty
  14. nifty_data = tv.get_hist(symbol='NIFTY', exchange='NSE', interval=Interval.in_1_hour, n_bars=1)
  15. # ITC
  16. ITC_data = tv.get_hist(symbol='ITC', exchange='NSE', interval=Interval.in_1_hour, n_bars=1)
  17. # RELIANCE
  18. RELIANCE_data = tv.get_hist(symbol='RELIANC', exchange='NSE', interval=Interval.in_1_hour, n_bars=1)
  19. print(nifty_data)
  20. print(ITC_data)
  21. print(RELIANCE_data)
  22. #this will return 3 different tables
  23. #i need data in 1 datafame

需要注意的是,代码中的库和变量名没有进行中文翻译,因为您要求只翻译代码部分,不要有其他内容。

英文:
  1. symbols = ["NIFTY", "ITC", "BBC", ....etc]
  1. from tvDatafeed import TvDatafeed, Interval
  2. tv = TvDatafeed()
  3. tv = TvDatafeed(username, password, chromedriver_path=None)
  4. tv = TvDatafeed(auto_login=true)
  5. nifty_data = tv.get_hist(symbol='NIFTY',exchange='NSE',interval=Interval.in_1_hour,n_bars=1)
  6. print(nifty_index_data)
  7. #result will be like this in pandas>>>>>>>
datetime symbol open high low close volume
2023-07-20 08:45:00 NSE:NIFTY 19943.15 19991.85 19935.45 19987.05 60215122.0

There is around 1060 stocks/symbols and we have to run script again-again to scrap different data for different symbols like this >>>

  1. # nifty
  2. nifty_data = tv.get_hist(symbol='NIFTY',exchange='NSE',interval=Interval.in_1_hour,n_bars=1)
  3. # ITC
  4. ITC_data = tv.get_hist(symbol='ITC',exchange='NSE',interval=Interval.in_1_hour,n_bars=1)
  5. # RELIANCE
  6. RELIANCE_data = tv.get_hist(symbol='RELIANC',exchange='NSE',interval=Interval.in_1_hour,n_bars=1)
  7. print(nifty_data)
  8. print(ITC_data)
  9. print(RELIANCE_data)
  10. #this will return 3 different tables
  11. #i need data in 1 datafame

I need all symbol data in one data frame like this>>>

datetime symbol open high low close volume
2023-07-20 03:45:00 NSE:BORORENEW 474.50 485.00 473.45 483.75 89517.0
2023-07-20 03:45:00 NSE:NIFTY 19841.15 19844.00 19784.55 19813.35 19934954.0

AND I ALSO DONT WAN'T TO RUN SCRIPT AGAIN-AGAIN BY CHANGING SYMBOL NAME

PLESE HELP THIS IS MY FIRST INTERVIEW PRESENTATION

**LIBARRY THAT I USED>>>TVDATAFEED **

答案1

得分: 1

以下是翻译好的部分:

  1. import pandas as pd
  2. symbols = ["NIFTY", "ITC", "RELIANC"]
  3. result = pd.concat([tv.get_hist(symbol=symbol, exchange='NSE', interval=Interval.in_1_hour, n_bars=1) for symbol in symbols])
英文:

Something like this should work:

  1. import pandas as pd
  2. symbols = ["NIFTY", "ITC", "RELIANC"]
  3. result = pd.concat([tv.get_hist(symbol=symbol,exchange='NSE',interval=Interval.in_1_hour,n_bars=1) for symbol in symbols])
  4. </details>

huangapple
  • 本文由 发表于 2023年7月20日 19:50:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定