[英]Only one element is being printed in a tuple
In 'stock_list' tuple there are multiple items(printing some of them for reference) but while running the for loop only first item is being printed. 在“ stock_list”元组中,有多个项目(打印其中一些以供参考),但是在运行for循环时,仅打印第一个项目。
Here is the output: 这是输出:
stock_list : ['G:\\ML\\Investing\\intraQuarter/_KeyStats\\a', 'G:\\ML\\Investing\\intraQuarter/_KeyStats\\aa', 'G:\\ML\\Investing\\intraQuarter/_KeyStats\\aapl', 'G:\\ML\\Investing\\intraQuarter/_KeyStats\\abbv', 'G:\\ML\\Investing\\intraQuarter/_KeyStats\\abc', 'G:\\ML\\Investing\\intraQuarter/_KeyStats\\abt', 'G:\\ML\\Investing\\intraQuarter/_KeyStats\\ace', 'G:\\ML\\Investing\\intraQuarter/_KeyStats\\aci', 'G:\\ML\\Investing\\intraQuarter/_KeyStats\\acn'] stock_list: ['G:\\ ML \\ Investing \\ inQuarter / _KeyStats \\ a','G:\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ aa','G:\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ aapl','G :\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ abbv','G:\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ abc','G:\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ abt','G:\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ ace','G:\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ aci','G:\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ acn']
each_dir: G:\\ML\\Investing\\intraQuarter/_KeyStats\\a each_dir: G:\\ ML \\ Investing \\ intraQuarter / _KeyStats \\ a
import pandas as pd
import os
import time
import datetime as datetime
path = "G:\ML\Investing\intraQuarter"
def Key_Stats(gather="Total Debt/Equity (mrq)"):
statspath = path+'/_KeyStats'
stock_list = [x[0] for x in os.walk(statspath)]
print(stock_list[1:10])
for each_dir in stock_list[1:]:
print(each_dir)
each_file = os.listdir(each_dir)
ticker = each_dir.split("_KeyStats\\")[1]
if len(each_file) > 0:
#parsing time from the html file
for file in each_file:
date_stamp = time.strptime(file, '%Y%m%d%H%M%S.html')
unix_time = time.mktime(date_stamp)
#print(date_stamp, unix_time)
full_file_path = each_dir+'/'+file
source = open(full_file_path, 'r').read()
value = source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0]
#print(ticker+":", value)
#time.sleep(15)
return
Key_Stats()
The return
on the last line of function definition is within the for loop, because of this the function will return on the first iteration, and further iterations will never happen. 函数定义的最后一行的
return
在for循环内,因此,该函数将在第一次迭代时返回,并且永远不会发生进一步的迭代。 Actually in python you do not need to write return at the end of the function, it will default to returning None
. 实际上,在python中,您不需要在函数末尾编写return,它默认将返回
None
。
or change the identation: 或更改标识:
def Key_Stats(gather="Total Debt/Equity (mrq)"):
statspath = path+'/_KeyStats'
stock_list = [x[0] for x in os.walk(statspath)]
print(stock_list[1:10])
for each_dir in stock_list[1:]:
print(each_dir)
each_file = os.listdir(each_dir)
ticker = each_dir.split("_KeyStats\\")[1]
if len(each_file) > 0:
#parsing time from the html file
for file in each_file:
date_stamp = time.strptime(file, '%Y%m%d%H%M%S.html')
unix_time = time.mktime(date_stamp)
#print(date_stamp, unix_time)
full_file_path = each_dir+'/'+file
source = open(full_file_path, 'r').read()
value = source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0]
#print(ticker+":", value)
#time.sleep(15)
return
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.