В кортеже печатается только один элемент - PullRequest
0 голосов
/ 17 марта 2019

В кортеже stock_list есть несколько элементов (некоторые из них печатаются для справки), но при запуске цикла for печатается только первый элемент.

Вот вывод:

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()

1 Ответ

1 голос
/ 18 марта 2019

return в последней строке определения функции находится внутри цикла for, поэтому функция вернется на первой итерации, и дальнейшие итерации никогда не произойдут. На самом деле в python вам не нужно писать return в конце функции, по умолчанию возвращается None.

или измените обозначение:

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...