简体   繁体   English

元组中仅打印一个元素

[英]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.

相关问题 如何创建只有一个元素的元组 - How to create a tuple with only one element 当有许多其他实例时,仅打印一个实例 - Only one instance being printed when there are many others Python:只有一个元素返回为元组,由find_closest返回 - Python : Only one element return in tuple return by find_closest python函数产生元组,只需要一个元素 - python function yields tuple and only one element is wanted 当我使用元组作为字典的键时,我可以只使用元组键的一个元素来搜索数据吗? - when I use a tuple for a key of a dictionary, can I search data with only one element of the tuple key? HTML 元素未使用 python's.text 打印 - HTML element not being printed with python's .text 仅提取元组的第一个元素 - Extract only first element of tuple 将张量元组传递给多个损失函数时,Tensorflow/keras 仅使用.fit 传递元组中的一个张量 - Tensorflow/keras when passing tuple of tensors to multiple loss functions, only one tensor in tuple is being passed using .fit 将一个元素的列表更改为元组 - Change list with one element to tuple ttk.treeview图表上每2秒输出三次的数据不是逐行打印的-第三次后仅一次打印 - Data outputted three times after every 2 seconds isn't being printed line by line on ttk.treeview chart-only printed in one burst after the third time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM