我在这里遇到一些错误,但我认为这是因为大熊猫没有导入,因为它是灰色的。如果这是问题,我该如何解决这个问题呢?
C:\阿纳康达\ python.exe C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py Traceback(大多数) 最近的呼叫最后):文件 “C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py”,第38行,in Key_Stats()文件“C:/Users/nickd/Documents/SKLEARN-STOCKS/stock-mach.py”,第12行,in Key_Stats df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])NameError:全局名称'pandas'未定义
使用退出代码1完成处理
import pandas as pd
import os
import time
from datetime import datetime
#location of the data files
path = 'C:\Users\nickd\Documents\SKLEARN-STOCKS'
#what specific field do you want to grab and in all files in that directory
def Key_Stats(gather="Total Debt/Equity (mrq) "):
statspath = path+'/_KeyStats'
stock_list = [x[0] for x in os.walk(statspath)]
df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])
for each_dir in stock_list[1:5]:
each_file = os.listdir(each_dir)
ticker = each_dir.split("//")[1]
if len(each_file) >0:
for file in each_file:
date_stamp = datetime.strptime(file, '%Y%m%d%H%M%S.html')
#unix_time = time.mktime(date_stamp.timetuple())
print(date_stamp,unix_time)
full_file_path = each_file+'/'+file
#print(full_file_path)
source = open(full_file_path, 'r').read()
#print(source)
try:
value = float(source.split(gather+':</td><td class="yfnc_tabledata1">')[1].split('</td>')[0])
df = df.append({'Date':date_stamp,'Unix':unix_time,'Ticker':ticker,'DE Ratio':value,}, ignore_index = True)
except Exception as e:
pass
#print(ticker+":",value)
save = gather.replace(' ','').replace(')','').replace('(','').replace('/',''+('.csv')
print(save)
df.to_csv(save)
#time.sleep(15)
Key_Stats()
答案 0 :(得分:4)
您已将其导入为
import pandas as pd
并致电
#pandas
df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])
您可以更改
import pandas as pd
至import pandas
或df = pandas.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])
至df = pd.DataFrame(columns = ['Date','Unix','Ticker','DE Ratio'])
。修改强>
错误是由于缺少)
save = gather.replace(' ','').replace(')','').replace('(','').replace('/',''+('.csv'))
答案 1 :(得分:1)
您将pandas导入为pd
。要么全程引用它,要么从导入中删除as pd
。
(另外,永远不会做except Exception... pass
。你会吞下各种错误,永远不知道它们是什么。如果你不能处理他们根本不抓住他们。)