一行代码正在执行,没有错误,但未返回任何输出

时间:2019-03-24 11:13:32

标签: python datetime

第二个“ try”块中的行正在执行,没有任何错误,但是它没有打印“ sp500_date”的值,并且正在创建一个空的数据集。没有第二组try-except块,代码可以正常工作,并且正在创建必要的输出

我在'sp500_value'行之前尝试了一条打印语句,打印效果很好

# -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""

import pandas as pd
import os
import time
import datetime as datetime
import time

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)]
    df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio','Price','SP500'])
    sp500_df = pd.read_csv("YAHOO-INDEX_GSPC.csv")

    for each_dir in stock_list[1:]:
        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",encoding='utf-8').read()
                try:
                    value = source.replace('\n','').split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0]

                    try:
                        sp500_date = datetime.fromtimestamp(unix_time).strftime('%m/%d/%Y')
                        print(sp500_date)
                        row = sp500_df[sp500_df['Date'] == sp500_date]
                        sp500_value = float(row["Adj Close"])
                    except:
                        sp500_date = datetime.fromtimestamp(unix_time-259200).strftime('%Y-%m-%d')
                        row = sp500_df[sp500_df['Date'] == sp500_date]a
                        sp500_value = float(row["Adj Close"])

                   # stock_price = source.replace('\n','').split('</small><big><b>')[1].split('</b></big>')[0]    
                   # print(stock_price, ":", ticker)
                    df = df.append({'Date':date_stamp, 'Unix':unix_time, 'Ticker':ticker, 'DE Ratio':value}, ignore_index=True)


                except Exception as e:
                    pass


    save = gather.replace(' ', '').replace(')','').replace('(','').replace('/','')+('.csv')
    print(save)
    df.to_csv(save)
    print(df)                


Key_Stats()

0 个答案:

没有答案
相关问题