简体   繁体   中英

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.

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']

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. Actually in python you do not need to write return at the end of the function, it will default to returning 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM